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 badges1 Answer
Reset to default 7Two things:
- You need to pass your field info to your
<input>
.
<input
type="text"
{...title} // <-------- that (it contains the "name" prop)
className="form-control"/>
- 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
版权声明:本文标题:javascript - redux-form: Dynamically defining handleSubmit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744807411a2626231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论