简体   繁体   中英

redux-form: Dynamically defining handleSubmit

Since I am pretty new to the React ecosystem my description and way of doing things may be way off but I hope you can follow my issue.

I have a parent Component that gets a form injected from the router and maps state and the action creators to the properties.

Container.js

import * as actionCreators from "../actionCreators";

export default class ComponentA extends React.Component {

  render() {
    return (
      <div className="ComponentA">
        {this.props.children} //<--Form
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    listItems: state.get("listItems")
  };
}

export const Container = connect(mapStateToProps, actionCreators)(ComponentA);

The component that gets render with {this.props.children} is the following form.

Form.js

class Form extends React.Component {

  static propTypes = {
    fields: PropTypes.object.isRequired,
    handleSubmit: PropTypes.func.isRequired
  };   

  render() {
    const { fields: {title, description}, handleSubmit} = this.props;
    return (
      <div className="create-project-form">
        <h1>Create Project</h1>
        <form onSubmit={handleSubmit}>
          <label htmlFor="title">Title</label>
          <input type="text" name="title" id="title" className="form-control"/>
          <label htmlFor="description">Description</label>
          <textarea name="description" id="" cols="30" rows="10" className="form-control"></textarea>
          <button className="btn btn-danger" onClick={handleSubmit}>Create</button>

        </form>
      </div>
    )
  }
}

export default connectReduxForm({
  form: "from",
  fields: ["title", "description"]
})(Form);

Router

const routes = <Route component={App}>
  <Route path="/" component={Container}/>
  <Route path="/a" component={Container}>
    <Route path="/a/create" component={Form}/>
  </Route>
</Route>;

render(
  <div>
    <Provider store={store}>
      <Router>{routes}</Router>
    </Provider>
  </div>,
  document.getElementById("content"));

The Problem is handleSubmit is not undefined, but it is non of my actions. I actually don't expect that it is magically set to the correct actionCreator but how do I pass in the function? I tried the action name instead of handleSubmit but then the function is undefined. Every example I saw passes the handleSubmit function manually into the Form component, but I can't do that because the Form is set by the Router.

thanks

Two things:

  1. You need to pass your field info to your <input> .

<input type="text" {...title} // <-------- that (it contains the "name" prop) className="form-control"/>

  1. You can pass any anonymous function to handleSubmit :

<form onSubmit={handleSubmit(data => { // do your submit stuff here and return a Promise if you want the // this.props.submitting flag to be set for the duration of the promise })}>

Does that help? See also .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM