我以这种方式在父组件中创建我的组件:
renderRow(row){
var Buttons = new Array(this.props.w)
for (var i = 0; i < this.props.w; i++) {
var thisButton=<FieldButton handler={this.actionFunction} key={'row'+ row +'col'+ i}></FieldButton>
Buttons.push(thisButton)
}
return Buttons
}
renderField(){
var Field = new Array(this.props.h);
for (var i = 0; i < this.props.h; i++) {
var row = this.renderRow(i);
Field.push(<View key={i} style={styles.fieldWrap}>{row}</View>);
}
this.setState({field: Field})
}
目标:
我必须更改 FieldButton 组件的状态。
我的问题:
更改 Childs 组件状态的正确方法是什么?
第一次尝试:
我试图将一个props绑定到父组件的状态:
<FieldButton color={this.state.color} handler={this.actionFunction} key={'row'+ row +'col'+ i}></FieldButton>
我还实现了 componentWillReceiveProps 方法,但从未调用过。
我试图将孩子移到 for 循环之外并且它起作用了。
第二次尝试:
我尝试使用 refs:这个问题。