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?
Sudhama
Founder, www.muzenly.com
Mayank Chandola
Yes the component is listening to the store but
showAddPostis stored in the internal state ofPostContainercomponent.