reactjs - 反应原生 | 如何创建动态状态(钩子)?
问题描述
如果提供了控制,我必须生成一个文本输入。我需要将状态放入 value 和 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")}
/>
)}
})
解决方案
试试这个方法
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 });
推荐阅读
- c# - MS Graph:Azure AD - 如何在用户列表中找到源列?
- c# - 如何克服 C# 中的 POST 请求 404 错误?
- python - 模型中自动生成的字段
- c# - Manatee.Json 获得人类可读的 SchemaValidationResults 输出
- r - 如何将 tibble 打印到具有良好格式的文本文件?
- angular - 在 AG 网格的单元格中双击编辑时更改列宽
- apache-spark - 在火花中,他们在附加新行时是否可以替代 union() 函数?
- django - Django:.count() 是否强制查询集评估
- reactjs - 如何根据选中的复选框(反应)切换按钮类名?
- angular - 如何强制在 Angular 中渲染相同的输入?