我正在将Formik与数组配合使用,其中的项是从父级传递并按如下方式检索的:
updateState (){
this.state.choices = this.props.choices
this.state.questionId = this.props.questionId
}
render() {
this.updateState()
var choices = this.state.choices
console.log(choices)
return ( ...
我最初将值初始化为空或0:
constructor(props){
super(props);
this.state = {
choices : [],
questionId: 0
};
}
虽然这看起来应该可行,但我收到一个错误,即组件正在更改要控制的文本类型的不受控制的输入。了解这是由于我使用this.state而引起的,但我不确定如何实际解决此问题。
到目前为止,自从我使用Formik以来,我所做的就是将导出更改为以下形式:
export default withFormik({
mapPropsToValues: (props) => ({
choices: [{
id: '',
value: '',
weight: '',
impact: ''}]
}),
})(Choices)
不清楚我是否应该绘制道具,还是应该使用类似的东西:
export default withFormik({
mapPropsToValues: (props) => ({
id: '',
value: '',
weight: '',
impact: ''
}),
})(Choices)
我所知道的是,我无法单击以将一个新对象推入正在使用的阵列上,因此该功能基本上被冻结,直到我能找出不受控制的输入元素的状态为止。
知道我要去哪里哪里吗?
答案 0 :(得分:0)
修复HTML和{choices [index] .id}位可以清除此错误。
例如:
<div className="col">
<label htmlFor={choices[index].id}>{choices[index].id}</label>
<Field name={choices[index].id} placeholder={choices[index].value} type="text"/>
<ErrorMessage name={choices[index].id} component="div" className="field-error" />
</div>