Clear Redux-Form Fields after submitting

user7334203 picture user7334203 · Mar 2, 2017 · Viewed 17.4k times · Source

Hello and have a nice day, i'm new to react and redux and in my project we are not using statefull components, just stateless with containers. i have a redux form with four fields. When i submit my form i want the fields to get cleared. I see something about dispatch(reset('myForm'). My question is where i have to put it? In my container and passing it as prop to my component?

My container is like this:

const mapDispatchToProps = dispatch => (
  {
    submitOrdersTradesSearch: (formSearchOrdersTradesData) => {
      dispatch(searchOrdersTrades(formSearchOrdersTradesData));
    }
  }
);

const OrdersTradesSearchForm = reduxForm({
  form: 'ordersTradesSearchForm',
})(OrdersTradesSearch);

const OrdersTradesSearchContainer =
  connect(null, mapDispatchToProps)(OrdersTradesSearchForm);

export default OrdersTradesSearchContainer;

Answer

Andy_D picture Andy_D · Mar 3, 2017

You can use onSubmitSuccess with reset.

import { reset, reduxForm } from 'redux-form';

const afterSubmit = (result, dispatch) =>
  dispatch(reset('ordersTradesSearchForm'));

const OrdersTradesSearchForm = reduxForm({
  form: 'ordersTradesSearchForm',
  onSubmitSuccess: afterSubmit,
})(OrdersTradesSearch);