简体   繁体   中英

How to update the state of a field programmatically using redux-form

I currently have 2 inputs which look like this: 在此处输入图片说明 I'm trying to update the end date input based on the first date input. Example if I input 08/15/2018 inside start date input I expect end date input to equal 08/15/2018.

My current code looks like this for end date input:

<Field
    component="input"
    format={(value, name) => {
       if (startDate.length && name === "start_date") {
          return startDate;
       }
       return value;
    }}
    name="end_date"
    onChange={onDateChange}
    type="date"
/>
  • The variable startDate captures the input from start date input

  • The current code is able to display the date under end date input, however, it is not updating the redux field - it remains undefined.

How can I display the data and also save it in redux form?

import { getFormValues, change } from 'redux-form';

Fetch value of field first

const mapStateToProps = state => ({ 
    formValues: getFormValues('<formname>')(state) || {},
});

Access that field inside above object received. formValues.

You can use change to change value in store.

change(field:String, value:any) 

export const mapDispatchToProps = dispatch => ({ 
setDate: value => dispatch(change('<formName>', value, null)),
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM