nat*_*iyu 6 javascript android react-native
我是新手ReactNative,我收到了错误undefined is not a object,我this.state.rows试图View在用户点击时动态添加Button.我尝试使用这个,而不是使用getInitialState我使用constructor(props)但我继续有上述错误.这是我的代码
constructor(props) {
super(props);
this.state = {
rows: [],
};
}
addRow(){
this.state.rows.push(index++)
this.setState({ rows: this.state.rows })
}
render(){
let contactView = () => {
return <View>
<AddComponent />
</View>
}
return(
<View>
<View>
{contactView}
</View>
<TouchableHighlight onPress={ this.addRow } >
<Text>Add</Text>
</TouchableHighlight>
</View>
);
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
小智 19
你需要this像这样绑定到addRow函数:
<TouchableHighlight onPress={ this.addRow.bind(this) } >
<Text>Add</Text>
</TouchableHighlight>
Run Code Online (Sandbox Code Playgroud)
或者创建一个匿名函数来自动绑定它:
<TouchableHighlight onPress={ () => this.addRow() } >
<Text>Add</Text>
</TouchableHighlight>
Run Code Online (Sandbox Code Playgroud)
有关解释,请参阅此处.
| 归档时间: |
|
| 查看次数: |
5674 次 |
| 最近记录: |