Javascript 带Redux表单的Redux thunk-不发送
下面的帖子很长,但并不复杂! 我已设置我的表单: NewCommentForm组件Javascript 带Redux表单的Redux thunk-不发送,javascript,redux,redux-form,Javascript,Redux,Redux Form,下面的帖子很长,但并不复杂! 我已设置我的表单: NewCommentForm组件 class NewCommentForm extends Component { render() { const { handleSubmit } = this.props; return ( <form onSubmit={handleSubmit}> <Field component="inpu
class NewCommentForm extends Component {
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit}>
<Field component="input" type="text" name="title"/>
<Field component="textarea" type="text" name="content"/>
</form>
)
}
}
const mapStateToProps = (state) => ({})
// Actions are imported as 'import * as action from '../actions/comments'
NewCommentForm = connect(mapStateToProps, actions)(NewCommentForm)
NewCommentForm = reduxForm({
form: 'newComment',
onSubmit: actions.postComment // This is the problem!
})(NewCommentForm);
class RemoteSubmitButton extends Component {
render() {
const { dispatch } = this.props;
return (
<button
type="button"
onClick={() => dispatch(submit('newComment'))}>Submit</button>
)
}
}
RemoteSubmitButton = connect()(RemoteSubmitButton);
从另一个文件获取其api.postComment
:
export const postComment = (comment) => {
return axios.post(post_comment_url, {
comment
}).then(response => {
return response;
});
}
我的商店中有redux thunk
设置:
import thunk from 'redux-thunk';
const configureStore = (railsProps) => {
const middlewares = [thunk];
const store = createStore(
reducers,
railsProps,
applyMiddleware(...middlewares)
);
return store;
};
为什么在使用
RemoteSubmitButton
提交表单后,postComment
函数的第二部分从未被调用?我做错了什么?问题是因为您试图使用与react redux
connect
未连接的操作。您必须在连接到redux的组件中使用它。我希望是这样。但是增加捕获量根本改变不了什么。没有显示日志,但我仍然只得到Post comment-first
messegeThanks进行编辑。我将this.props.handleSubmit(this.props.postComment)
传递给表单onSubmit
,但随后出现了一个错误:您必须传递handleSubmit()onSubmit函数或作为prop传递onSubmit
。我发现了您的问题,但现在您必须学习如何使用react表单阅读文档来实现这一点。我想这可能会对您有所帮助。但问题是,postComment
函数是使用reduxconnect
连接的。这是我在回答您的第二个问题时写的。我终于找到了方法——我必须通过提交
到纽康门表单
比如:
。
export const postComment = (comment) => {
return axios.post(post_comment_url, {
comment
}).then(response => {
return response;
});
}
import thunk from 'redux-thunk';
const configureStore = (railsProps) => {
const middlewares = [thunk];
const store = createStore(
reducers,
railsProps,
applyMiddleware(...middlewares)
);
return store;
};