Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Redux not updating right after dispatch in functional components

Recently I decided to change my code from class to functional components. I'm using redux, axios and axiosMiddleware. Since I started changing to functional components I noticed something strange. See the following example which is a request using a dispatch (I'm using connect and mapDispatchToProps):

const getUserActivity = () => {
 await props
      .get(displayDateString)
      .then((response) => {
        console.log(response); //prints response
        console.log(props.activity); //prints part of the redux that should be updated
      })
      .catch((error) => console.log('error ' + error));
}
const mapDispatchToProps = (dispatch) => ({
  get: (date) => dispatch(ActivityReportActions.get(date))
})

My problem here is, when I get the response after "then" the redux does not get immediately updated, only afterwards. "console.log(props.activity)" prints the previous state, and in some cases I prefer to access the redux right after the response. Does anyone have any idea why this is happening in functional components and not in class components? If yes, is there a solution which does not require using useEffect()?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is impossible to read new values from props immediately after dispatching. React hasn't had a chance to re-render yet.

If you need to read the latest state immediately after a dispatch, you'll have to write the logic as a thunk, which has access to getState:

https://redux.js.org/usage/writing-logic-thunks#accessing-state

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!