调度并在另一个组件中返回值

时间:2018-12-02 09:00:51

标签: reactjs react-redux

您好,我正在撰写帖子的评论部分。

我这样创建减速器name="file"

addcomment.js

我有var defaultCommentState = { login_status: false, author_name: '', author_email: '', author_url: 'https://google.com', content: '', post:null, parent:0 } const addComment = (state = defaultCommentState, action) => { switch(action.type) { case 'ADD_COMMENT': return { ...state, login_status: action.login_status, author_name: action.author_name, author_email: action.author_email, content: action.content, post: action.post, parent: action.parent } default: return state; } } export default addComment; ,其中包含以下内容:Detail.js

在这个问题上,我只关注Post detail + List of comments + Comment Form(因为List of comments包含commentForm的List of comments和主题注释的postId

对于评论的返回列表,我使用CommentList这样:

parentId

,您会看到我在这里做了class CommentList extends Component { constructor(props) { super(props); this.state = { comments: [], page: 1, loadedDone: false, error: null, replyPostId: '' } } componentDidMount() { const apicomment = CelestialSettings.URL.api + "/comments/?post=" + this.props.postid + "&parent=0&page=" + this.state.page; //Start Dispatch store.dispatch({type: 'ADD_COMMENT', post: this.props.postid}); ......

然后在dispatch中,将Comment Form与该组件连接。

store

我的问题是:

  • 我确实进行了调度,并返回了class CommentForm extends React.Component { constructor(props) { super(props); let user = JSON.parse(localStorage.getItem('user')); if (user && user.token) { this.state = { ...this.props.addComment, login_status : true, author_name: user.user_nicename, author_email: user.user_email, content: this.props.content, parent: 0 } }else{ this.state = { ...this.props.addComment, login_status: false, } } } ... ... export default connect(function(state) { return {addComment: state.addComment} })(withRouter(CommentForm)); ,但是为什么在我从评论表中调用它之前,它没有更新为post id

我该如何解决?

0 个答案:

没有答案