所以我有一个formik表单,我用this.state设置了输入值之一。
当页面呈现时,这可以正常工作。但是,我有一个click事件,它将状态更改为新值,但输入值未更新。我该怎么办?
代码:
this.state.address_1:'test'
initialValues={{
address_1: this.state.address_1,
}}
<span className='form-cell-wrapper'>
<label>Address Line 1</label>
<Field
name='address_1'
type='text'
style={{
borderColor:
errors.address_1 && touched.address_1 && "tomato"
}}
/>
</span>
addressClick = (e) => {
this.setState({ 'address_1': 'test 2' });
console.log('address change', this.state.address_1);
}
答案 0 :(得分:0)
在字段中添加
<Field
onChange={this.addressClick}
value={this .state.address_1}
name='address_1'
type='text'
style={{
borderColor:
errors.address_1 && touched.address_1 && "tomato"
}}
/>
and
addressClick = (e) => {
this.setState({ 'address_1': e.target.value });
console.log('address change', this.state.address_1);
}
如果不起作用,请尝试尝试在提交时告诉我