T
T
tron212022-03-17 19:17:11
React
tron21, 2022-03-17 19:17:11

How does the React-Redux reducer work?

Good afternoon! I started learning Redux and then a question arose - my application is written using React / Redux . There is a button on clicking on which certain actions are performed

const changeAmount = (top) => {
    dispatch({ type: DATA_FOR_REPOSTS_CHANGE_TOP, data: top });
    dispatch(getListReportsApiAction(isDataForReports));
  };


The first dispatcher overwrites some of the data in the reducer , and the second one calls action , which accesses exactly the data that was overwritten. But he receives the correct data only from 2 times and so constantly. At the same time, if the second dispatch is put into useEffect and the dependency on changing the data (which changes in 1 dispatch), then everything works fine.

useEffect(() => {
    dispatch(getListReportsApiAction(isDataForReports));
    console.log(isReports);
  }, [isDataForReports]);


Please tell me what to look at or how to configure the function so that it receives updated data without using useEffect

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wonderingpeanut, 2022-03-27
@timofeus91

When calling the second dispatcher, the state does not yet have the data that you sent with the first dispatcher
Solution - use thunk or, as you did, useEffect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question