由于我对反应生态系统非常陌生,我的描述和做事方式可能还很遥远,但我希望你能关注我的问题。
我有一个父组件,它从路由器注入一个表单,并将状态和动作创建者映射到属性。
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);使用{this.props.children}呈现的组件如下所示。
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);路由器
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"));问题是handleSubmit不是没有定义的,但它不是我的行为。实际上,我并不期望它被神奇地设置为正确的actionCreator,但是如何传递函数呢?我尝试了动作名而不是handleSubmit,但是函数没有定义。我看到的每个示例都将handleSubmit函数手动传递到表单组件中,但我无法做到这一点,因为表单是由路由器设置的。
谢谢
发布于 2015-11-03 21:20:25
有两件事:
<input>。<input type="text" {...title} // <-------- that (it contains the "name" prop) className="form-control"/>
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 })}>
有帮助吗?See also。
https://stackoverflow.com/questions/33509007
复制相似问题