admin管理员组文章数量:1426655
I have an onBlur on input text fields which auto-saves the record; The data structure is one document which contains an array of items. If i change an item's first column and then the second column quickly, the first column's save would cause an update which would reload both input fields.
How can i prevent the first save from reloading the entire row so both column values get saved?
Here's the code snippet (I mimiced a server save with a setTimeout
):
class Store {
static doc = {title: "test title", items: [{id: 1, one: "aa", two: "bbb"}, {id: 2, one: "yyy", two: "zzz"}]};
static getDocument() {
return this.doc;
}
static saveDocument(doc) {
this.doc = doc;
}
static updateItemInDocument(item, callback) {
var foundIndex;
let updatedEntry = this.doc.items.find( (s, index) => {
foundIndex = index;
return s.id === item.id;
});
this.doc.items[foundIndex] = item;
setTimeout(() => { console.log("updated"); callback(); }, 1000);
}
};
const Row = React.createClass({
getInitialState() {
return {item: this.props.item};
},
update() {
let document = Store.getDocument();
let updatedEntry = document.items.find( (s) => {
return s.id === this.props.item.id;
} );
this.setState({ item: updatedEntry});
},
handleEdit() {
this.setState({item: {
id: this.props.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
});
},
handleSave() {
Store.updateItemInDocument(this.state.item, this.update);
},
render() {
let item = this.state.item;
console.log(item);
return <tr> <p>Hello</p>
<input ref="one" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.one} />
<input ref="two" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.two} />
</tr>;
}
});
const App = React.createClass({
render() {
let rows = Store.getDocument().items.map( (item, i) => {
return <Row key={i} item={item} />;
});
return <table>
{rows}
</table>;
}
});
ReactDOM.render(
<App />,
document.getElementById("app")
);
I also have the code as a codepen:
I have an onBlur on input text fields which auto-saves the record; The data structure is one document which contains an array of items. If i change an item's first column and then the second column quickly, the first column's save would cause an update which would reload both input fields.
How can i prevent the first save from reloading the entire row so both column values get saved?
Here's the code snippet (I mimiced a server save with a setTimeout
):
class Store {
static doc = {title: "test title", items: [{id: 1, one: "aa", two: "bbb"}, {id: 2, one: "yyy", two: "zzz"}]};
static getDocument() {
return this.doc;
}
static saveDocument(doc) {
this.doc = doc;
}
static updateItemInDocument(item, callback) {
var foundIndex;
let updatedEntry = this.doc.items.find( (s, index) => {
foundIndex = index;
return s.id === item.id;
});
this.doc.items[foundIndex] = item;
setTimeout(() => { console.log("updated"); callback(); }, 1000);
}
};
const Row = React.createClass({
getInitialState() {
return {item: this.props.item};
},
update() {
let document = Store.getDocument();
let updatedEntry = document.items.find( (s) => {
return s.id === this.props.item.id;
} );
this.setState({ item: updatedEntry});
},
handleEdit() {
this.setState({item: {
id: this.props.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
});
},
handleSave() {
Store.updateItemInDocument(this.state.item, this.update);
},
render() {
let item = this.state.item;
console.log(item);
return <tr> <p>Hello</p>
<input ref="one" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.one} />
<input ref="two" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.two} />
</tr>;
}
});
const App = React.createClass({
render() {
let rows = Store.getDocument().items.map( (item, i) => {
return <Row key={i} item={item} />;
});
return <table>
{rows}
</table>;
}
});
ReactDOM.render(
<App />,
document.getElementById("app")
);
I also have the code as a codepen: http://codepen.io/tommychheng/pen/zBNxeW?editors=1010
Share Improve this question edited Jun 24, 2016 at 22:29 tommy chheng asked Jun 24, 2016 at 21:52 tommy chhengtommy chheng 9,22810 gold badges57 silver badges75 bronze badges 2-
item
always and only has the propertiesone
andtwo
? Or canitem
have more or less than those properties? – Ashitaka Commented Jun 24, 2016 at 22:42 - item has a fixed number of properties(just one and two) and input fields. – tommy chheng Commented Jun 26, 2016 at 23:57
1 Answer
Reset to default 3The issue stems from the fact that tabbing out of an input field fires both the onChange
and onBlur
handlers.
Maybe I'm misunderstanding your use case, but here's how I'd fix this issue:
const Row = React.createClass({
getInitialState() {
return { item: this.props.item };
},
handleSave() {
let item = {
id: this.state.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
Store.updateItemInDocument(item); // Is it really necessary to use the callback here?
},
render() {
let item = this.state.item;
return (
<tr>
<p>Hello</p>
<input ref="one" type="text" onBlur={this.handleSave} defaultValue={item.one} />
<input ref="two" type="text" onBlur={this.handleSave} defaultValue={item.two} />
</tr>
);
}
});
I switched from using the onChange
handler to using a defaultValue.
本文标签: javascriptHow do I autosave with multiple form fields in ReactStack Overflow
版权声明:本文标题:javascript - How do I auto-save with multiple form fields in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428085a2658205.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论