Why redux suggests to only connect to top level components?

后端 未结 5 409
悲哀的现实
悲哀的现实 2020-12-25 12:53

I\'m new to redux and react-redux, in the mean time I am trying to make a redux app.

I don\'t understand the statement on redux document:

Then

5条回答
  •  一生所求
    2020-12-25 13:30

    In some cases, you can use connect()ed components deeper down. I wouldn't interpret the documentation so strictly.

    Generally speaking, if you find yourself passing down too many props from your components and the components doing the passing aren't using those props, then they can be moved to a separate container component.

    If you find yourself constantly writing props down a component chain it might be time to add a container:

    // A blog post view component
    render () {
      const {post} = this.props;
    
      return (
        

    {post.title}

    ) }

    In this case, the post component doesn't have any use or need of any props used for . You might want to consider making an instead. The author belongs to the post, so you will need that bit of information. The rest can be computed using state in the container's mapStateToProps(state, ownProps) function where ownProps.author is the author object.

    Again, this is a contrived example, but it is really up to you in the end where the logic belongs.

提交回复
热议问题