我正在学习React书籍,看到这么简单的,但是不知道为什么[item]:value]后面有一个','
handleChange(item ,e) {
const {value} = e.target;
this.setState({[item]:value,});
}
render() {
const {name, age} = this.state;
return (
<div className="FormA">
name: <input value={name} onChange={this.handleChange.bind(this,'name')}/>
age: <input value={age} onChange={this.handleChange.bind(this,'age')}/>
</div>
);
}
最佳答案
在每个对象属性后面添加“,”被认为是最佳实践。您必须使用过 ESLint:https://eslint.org/docs/rules/comma-style
这只是一个语法问题,不会向您的代码添加任何内容。
关于React setState 中的 JavaScript 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092229/