【发布时间】:2018-08-14 17:55:38
【问题描述】:
我正在尝试更新处于父组件状态的数组。每个子组件都是通过父组件状态数组中的一个元素创建的。现在我有一个 onChange 处理程序,它位于我的 Parent 组件中,我将它传递给我的 Child 组件。
这里有一些与此有关的 sn-ps:
父母
class PropertyBar extends Component {
state = {
selectedCell: graph.selectedCell,
cellProperties: []
}
onChangeHandler(e) {
let cellProperties = [...this.state.cellProperties];
cellProperties[e.target.name] = e.target.value;
this.setState({cellProperties});
}
renderPropertyList() {
return this.state.cellProperties.map(key => {
return <PropertyBarItem name={key.nodeName} value={key.nodeValue} onChange={this.onChangeHandler}/>
})
}
}
孩子
class PropertyBarItem extends Component {
constructor(props) {
super(props);
}
onChangeHandler(e) {
if (this.props.onChangeHandler) {
this.props.onChangeHandler(e);
}
}
render() {
return (
<ListItem divider={true} className="property-bar-item">
<TextField
id="property"
label={this.props.name}
value={this.props.value}
margin="dense"
onChange={(e) => {this.onChangeHandler(e)}}
/>
</ListItem>
)
}
}
我认为我没有正确处理在子组件中传递的onChangeHandler,但我的逻辑也可能有误。
【问题讨论】:
-
在你的父母中,当你传入你的处理程序时,你将它作为'onChange'的道具传入 onChange={this.onChangeHandler} 如果你尝试 onChangeHandler={this.onChangeHandler} 会发生什么}
-
@Crawdingle 很好,所以现在它在我开始在文本字段中输入后崩溃并给我错误'无法读取未定义的属性'cellProperties''。不要分歧,但是
onChange是react中的保留字吗?
标签: javascript reactjs