admin管理员组

文章数量:1410689

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 ponent 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 ponent={App}>
  <Route path="/" ponent={Container}/>
  <Route path="/a" ponent={Container}>
    <Route path="/a/create" ponent={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 ponent, but I can't do that because the Form is set by the Router.

thanks

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 ponent 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 ponent={App}>
  <Route path="/" ponent={Container}/>
  <Route path="/a" ponent={Container}>
    <Route path="/a/create" ponent={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 ponent, but I can't do that because the Form is set by the Router.

thanks

Share Improve this question asked Nov 3, 2015 at 21:02 KenavRKenavR 3,89910 gold badges40 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.

本文标签: javascriptreduxform Dynamically defining handleSubmitStack Overflow