Philip-lf
Philip-lf

Reputation: 115

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

I currently have 2 inputs which look like this: enter image description here 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"
/>

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

Upvotes: 0

Views: 173

Answers (1)

Anil Kumar
Anil Kumar

Reputation: 2309

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

Upvotes: 1

Related Questions