Redux-form: How to access fields data in action?

Created on 12 Mar 2016  路  3Comments  路  Source: redux-form/redux-form

I am trying to access fields data in action computing theirs values into summary.

RequestForm = reduxForm({
  form:   'TrainingRequestForm',
  fields: [
    'section1.name',
    'section1.costs.overall',
    'section1.costs.food',
    'section1.costs.other',
  ],
},
(state, props) => {
    return {
        app: state.app
    }
},
(dispatch, props) => {
    return {
        onSubmit: (formData) => {
          dispatch({
            type:     'FORM_UPDATED_WITH_DATA',
            form:     formData
          })
        },
        onCostsUpdate: (fields) => {

            let _summary = 0;

            _.map(fields.section1.costs, (cost) =>  {
                let value = parseFloat(cost.value);
                console.log("MAP VALUE", value);

                _summary += ( ! isNaN(value)) ? value : 0;
            });

            dispatch({
              type:   'COSTS_UPDATED',
              summary: _summary
            })
        }
    }
})(RequestForm);

I've tried to bind fields to the action creator

class RequestForm extends Component {
  constructor(props) {
    super(props);

    this.onCostsUpdate = props.onCostsUpdate.bind(null, props.fields);

    // this.state = {};
    this.onCostsUpdate();
  }
...
...
}

Is there any realiable way to access fields values in action creator?

question

Most helpful comment

If you used thunk middleware, you could then use getValues(). That should do it.

All 3 comments

If you used thunk middleware, you could then use getValues(). That should do it.

Thanks.

costsUpdate: () => {
          dispatch((dispatch, getState) => {

            let _summary = 0;

            _.map(getState().form.TrainingRequestForm.section1.costs, (cost) =>  {
                let value = parseFloat(cost.value);
                console.log("MAP VALUE", value);

                _summary += ( ! isNaN(value)) ? value : 0;
            });

            dispatch({
              type:   'COSTS_UPDATED',
              summary: _summary
            })
          });
        },

works fine!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaraquistain picture jaraquistain  路  3Comments

shikelong picture shikelong  路  3Comments

wtfil picture wtfil  路  3Comments

ashwinvandijk picture ashwinvandijk  路  3Comments

2easy picture 2easy  路  3Comments