如果提供了控件,则必须生成一个textinput。我需要将状态放入值和onChange中。所以我必须产生一个动态状态。我该怎么做?
list.map(item =>{
//control true. i need creat textinput
if (control) {
//I have to create the state here (ex. const[item.name, `set${item.name}`]=useState())
return(
<TextInput
onChangeText={(text) => ?}
value={?}
placeholder={item.name}
keyboardType={(item.input_type === "tel")}
/>
)}
})
答案 0 :(得分:1)
尝试这种方式
constructor(props){
super(props);
this.state = {
textInput : [],
inputData : []
}
}
onTextChanged = (index, value) => {
const inputData = [...this.state.inputData];
inputData[index] = value;
}
const textInput = [...this.state.textInput];
list.map((item,index) =>{
//control true. i need creat textinput
if (control) {
textInput.push(
<TextInput
value={this.state.inputData[index] || ''}
placeholder={item.name}
onChangeText={(text) => this.onTextChanged(index, text)}
/>
);
}
});
this.setState({ textInput });