Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 639
user7334203 Avatar asked Mar 02 '17 09:03

user7334203


2 Answers

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);
like image 189
Andy_D Avatar answered Nov 03 '22 14:11

Andy_D


Here is another Way of doing the same thing. - Redux provides a predefined method to handle submit called handleFormSubmit. You can basically do the same thing in that method as well, No need to use an extra method.

import React from "react";
import { reset, reduxForm } from "redux-form";

class Form extends React.Component {
  onSubmit = (formValues, dispatch) => {
    dispatch(reset("streamsForm")); // string here is the name of the form, check the export default reduxForm below.
  };

  render() {
    return (
      <form onSubmit={this.props.handleSubmit(this.onSubmit)}>
        // Your Form related Fields
      </form>
    );
  }
}

export default reduxForm({
  form: "streamsForm"
})(Form);
like image 24
Abhishek Gangwar Avatar answered Nov 03 '22 12:11

Abhishek Gangwar