3

I have dispatched an action which doesn't contain any asynchronous code such as ajax call or anything which changes the reducer state. Can I access the changed reducer state in the very next line in the react app?

React APP

actions.setName('Test'); //this sets the name in state of reducer to Test
//Can I access the name in the next line
const name = this.props.name;
Vijay
  • 286
  • 1
  • 9

3 Answers3

1
let response = await this.props.appAction.processList(token, userName);
prodArray = this.props.processList;

This is my example code and it works. Maybe it can help you. await keyword works for me.

Shivansh Singh
  • 312
  • 1
  • 2
  • 12
  • Here you are explicitly making the response line of code synchronous by using await. But what I am asking conceptually is- would the reducer state change and the new state be available on reaching the next line without any of these promises/await – Vijay Sep 08 '17 at 09:49
  • Yes it should as redux functioning is synchronous. Here my appAction.processList is a network request that's why there is the need of await. – Shivansh Singh Sep 08 '17 at 10:06
  • The dispatch function is not synchronous. – Vijay Sep 08 '17 at 13:09
  • https://stackoverflow.com/questions/43276291/is-store-dispatch-in-redux-synchronous-or-asynchronous Maybe this helps you to understand – Shivansh Singh Sep 08 '17 at 13:11
1

The Redux store's state itself is immediately updated, because dispatching is synchronous. However, React's re-rendering is almost always asynchronous, so no, updates to props are not available immediately after an action was dispatched.

markerikson
  • 63,178
  • 10
  • 141
  • 157
0

Just discovered that we cannot have the state updated immediately and available as props in the next line.

The only way you get the updated state is when component re-renders. You cannot expect the next line to have the latest state at any time.

Look into this discussion for further understanding on how to go about solving this issue.

Click for the discussion thread

Vijay
  • 286
  • 1
  • 9