Sign in
Log inSign up
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?

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct