My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Why explicitly setState of prop variables when they should be bound to the store?

Why explicitly setState of prop variables when they should be bound to the store?

Sudhama's photo
Sudhama
·Apr 4, 2016

This is one thing I am not fully clear about although this is a pattern in many of the redux examples as well from the redux library.

I am using the mern-starter kit PostContainer as a reference to explain my question.

add(name, title, content) {
    this.props.dispatch(Actions.addPostRequest({ name, title, content }));
    this.setState({
      showAddPost: false,
    });
  }

function add() is called from the following logic in the render() method

<PostCreateView addPost={this.add}
            showAddPost={this.state.showAddPost}/>
          <PostListView posts={this.props.posts}/>
        </div>

My question is, why does add method need to call setState()?? Shouldn't the component be listening to the store and since we mapStateToProps, the state automatically have the latest value of the showAddPost from the store? What am I missing?