React Redux没有重新渲染,但状态被复制了?

时间:2018-04-18 05:15:48

标签: reactjs redux react-redux

我已阅读文档here,但在更新状态后,我无法让组件重新呈现。这些帖子正在添加中,我只需要手动重新发布该组件以使其显示,我缺少什么?

我在组件中有这个:

 class ListPosts extends Component {
  state = {
   open: false,
   body: '',
   id: ''
  }

  openPostModal = () => this.setState(() => ({
    open: true,
  }))

closePostModal = () => this.setState(() => ({
  open: false,
}))

  componentWillMount() {
    const selectedCategory = this.props.selectedCategory;
    this.props.fetchPosts(selectedCategory);
    }



  handleChange = (e, value) => {
    e.preventDefault();
   // console.log('handlechange!', e.target.value)
    this.setState({ body: e.target.value });
 };

submit = (e) => {
  // e.preventDefault();
  console.log(this.state.body)
  const body = this.state.body;
  const id = getUUID()
  const category = this.props.selectedCategory;
  const post = {
     id,
     body,
     category
  }
  this.props.dispatch(addPost(post))
  this.closePostModal()
}

然后在下面我将调度添加到道具......

const mapStateToProps = state => ({
 posts: state.postsReducer.posts,
 loading: state.postsReducer.loading,
 error: state.postsReducer.error,
 selectedCategory: state.categoriesReducer.selectedCategory,
 // selectedPost: state.postsReducer.selectedPost,
});

 function mapDispatchToProps (dispatch) {
   return {
   fetchPosts: (selectedCategory) => dispatch(fetchPosts(selectedCategory)),
   addPost: (postObj) => dispatch(addPost(postObj)),
  }
}

 export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
 )(ListPosts))

以下是reducer的代码:

 case C.ADD_POST :

  const hasPost = state.some(post => post.id === action.payload.postObj.id)

  console.log('caseADD_POST:', action.payload.postObj.id)
  return (hasPost) ?
     state :
     [
       ...state,
       post(null, action)
     ];

0 个答案:

没有答案
相关问题