我最近发现了redux-form
,看起来非常棒。
我正在尝试远程提交表格。我创建了一个单独的按钮redux-form
docs说:
class RemoteSubmitButton extends Component {
render() {
console.log("RSB:", submit('newComment'));
const { dispatch } = this.props;
return (
// newComment is the name of the form
<button
type="button"
onClick={() => dispatch(submit('newComment'))}>Submit</button>
)
}
}
// NewComment is my form component that I created
NewComment = reduxForm({
form: 'newComment',
onSubmit: postComment
})(NewComment);
和postComment函数如下所示:
export const postComment = (comment) => (dispatch, getState) => {
console.log("postComment - comment:", comment);
return api.postComment(comment).then(response => {
dispatch({
type: 'POST_COMMENT_SUCCESS',
response
});
});
}
提交表格后,我得到的绝对没有。问题是postComment
函数,特别是=> (dispatch, getState) =>
部分。没有它我可以看到日志,但我无法发送POST_COMMENT_SUCCESS
。
我该如何做到这一点?
修改
class NewComment extends Component {
render() {
return (
<div class="new-comment">
<form onSubmit={this.props.handleSubmit}>
<Field .../>
</form>
</div>
)
}
}