Sumit Wadhwa
Sumit Wadhwa

Reputation: 3217

confusion about actions in Redux

I'm not able to figure out whether Redux actions are synchronous or asynchronous. Consider this:

addBook () => {
  console.log( "current books:  ",this.props.books );

  const book = {
      id: 3,
      title: "Percy Jackson"
  };

  this.props.addBook(book); // firing off a new action [ is it synchronous or asynchronous ]

  console.log( "updated books:  ",this.props.books ); // shouldn't these be new props
}

From above, both current books and updated books are same, though UI is updated and I'm guessing new props from the store are also passed to this component via mapStateToProps method.

What am I missing?

Upvotes: 0

Views: 37

Answers (1)

jmargolisvt
jmargolisvt

Reputation: 6088

Firing the action is synchronous. The reducer however is async. You are getting the same value before and after because it is all happening in one, synchronous render cycle. After the reducer does its thing, your component gets updated as you've seen. You're never going to see the console.log statements reflect two different states in this case.

Upvotes: 1

Related Questions