Search code examples
submitresetredux-form

Clear Redux-Form Fields after submitting


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;

Solution

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