如何获取Formik以更新点击输入?

时间:2019-09-03 09:32:24

标签: reactjs formik

所以我有一个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);


    }

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);


    }

如果不起作用,请尝试尝试在提交时告诉我