【发布时间】:2020-09-02 22:55:57
【问题描述】:
我对 React Native 并不陌生。我有一个场景,我需要动态创建 TextInput 并从数组中绑定值。数组更新后,TextInput 的值不会更新。下面是我的代码。
constructor(props) {
super(props);
this.state = {
textInputValues: [],
textInput: [],
samplearray://gets an array from the JSON
}
componentDidMount() {
this.setTextInputValue();
this.prepareTextBox();
}
setTextInputValue() {
let textInputValues = this.state.textInputValues;
this.state.samplearray.map(() => {
textInputValues.push("") //default value
this.setState({ textInputValues })
})
}
prepareTextBox() {
let textInput = this.state.textInput;
this.state.samplearray.map((value, index) => {
textInput.push(<TextInput style={styles.textBox} value={this.state.textInputValues[index]} key={index} />);
})
this.setState({ textInput })}
在 render 方法中渲染 TextBox 的代码。
{ this.state.textInput.map((value, index) => {
return value
})}
我有一个 this.state.textInputValues 数组值被更改的按钮。但是这种变化并没有反映在 TextInput 中。自2天以来一直坚持这一点。任何帮助表示赞赏,在此先感谢。
【问题讨论】:
标签: reactjs react-native mobile