Javascript 在使用map函数时,如何将每个状态动态绑定到每个复选框
我正在做类似的事情,但是值被当作字符串,而不是带有布尔值的变量Javascript 在使用map函数时,如何将每个状态动态绑定到每个复选框,javascript,react-native,checkbox,Javascript,React Native,Checkbox,我正在做类似的事情,但是值被当作字符串,而不是带有布尔值的变量 {question.data.map((value, i) => ( <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}> <CheckBox value={`this.state.checked${i}`} onVa
{question.data.map((value, i) => (
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
<CheckBox value={`this.state.checked${i}`} onValueChange={() => this.toggleCheckBox(i)} />
<Text>{value.question}</Text>
</View>
))
}
需要帮助。问题在于围绕此.state添加字符串插值
toggleCheckBox(i) {
this.setState({ [`checked${i}`]: !this.state[`checked${i}`] });
}
{question.data.map((value, i) => (
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
<CheckBox value={this.state[`checked${i}`]} onValueChange={() => this.toggleCheckBox(i)} />
<Text>{value.question}</Text>
</View>
))
}
切换复选框(i){
this.setState({[`checked${i}`]:!this.state[`checked${i}`]});
}
{question.data.map((值,i)=>(
this.toggleCheckBox(i)}/>
{value.question}
))
}
这很有效。。。非常感谢你的帮助。祝你今天愉快。:)
toggleCheckBox(i) {
this.setState({ [`checked${i}`]: !this.state[`checked${i}`] });
}
{question.data.map((value, i) => (
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
<CheckBox value={this.state[`checked${i}`]} onValueChange={() => this.toggleCheckBox(i)} />
<Text>{value.question}</Text>
</View>
))
}