Your both dispatch are called after first render so even before your second render value is 0 so your second useEffect won't be able detect change as there is no change.

Let's see what is happening in your render method

First Render:

a = 0

first useEffect: dispatch({ a : 1 })

second useEffect: dispatch({ a : 0 })

so now in your redux store a is 0.

Second Render

a = 0

first useEffect: doesn't run as there is no dependency

second useEffect: doesn't run as a hasn't changed.

Answer from Amit Chauhan on Stack Overflow
Top answer
1 of 2
5

Your both dispatch are called after first render so even before your second render value is 0 so your second useEffect won't be able detect change as there is no change.

Let's see what is happening in your render method

First Render:

a = 0

first useEffect: dispatch({ a : 1 })

second useEffect: dispatch({ a : 0 })

so now in your redux store a is 0.

Second Render

a = 0

first useEffect: doesn't run as there is no dependency

second useEffect: doesn't run as a hasn't changed.

2 of 2
1

PLEASE, stop using

 useSelector(state => state.mainReducer);

it doesn't make any sense

there should be a simple state transformation (subselection)

const a = useSelector(state => state.a)

taken directly from redux docs:

const counter = useSelector(state => state.counter)  

update

you can see effect (from store change) with slightly changed component

function MyComponent(props) {
  const a = useSelector(state => state.a);
  const dispatch = useDispatch(); 

  console.log('render: ', a);

  useEffect(() => {
    console.log('use effect: ', a);
    dispatch({ type: 'A', payload: a });
  }, [a]) 

  useEffect(() => {
    console.log('did mount: ', a);
    dispatch({ type: 'A', payload: 1 })
  }, []); 

  return (<View style={styles.container}>
    <Text style={styles.text}>{a}</Text>
  </View>);
};

It should result in log:

  • render: 0 // initial state
  • use effect: 0 // first effect run
  • // dispatch 0 ... processed in store by reducer but results in the same state ...
    // ... and in our rendering process we still working on an 'old' a readed from state on the beginning of render
  • did mount: 0 // 'old' a
    // dispatch 1 ... changed state in redux store
  • .... rendered text 0
    ...
    ...

  • // useSelector forces rerendering - change detected

  • render: 1 // latest dispatched value, processed by reducers into new state, rereaded by selector
  • use effect: 1 // useEffect works AS EXPECTED as an effect of a change
  • .... rendered text 1

...
...

  • no more rerenderings - latest dispach not changed state

Of course dispatch from other component will force update in this component ... if value will be different.

Discussions

Bug: useEffect closure not called when dependencies changed
I run into a very weird issue where the dependency of useEffect changes, but its closure is not called. The component renders correctly with the updated value due to a state change of a parent rout... More on github.com
🌐 github.com
17
March 8, 2022
Bug: useEffect not triggering on [deps] change
I stumbled upon this issue which initially I thought is React bug, but upon further investigation React Query seems like the next best candidate. Minimal project is created to demonstrate the probl... More on github.com
🌐 github.com
26
September 2, 2021
useEffect that's not executed at mount but only when dependency array changes
This is usually indicative of a bad use of useEffect. What is causing the values in the dependency array to change? You can put this code inside that event handler instead. More on reddit.com
🌐 r/reactjs
21
14
May 29, 2024
useEffect not running properly
Just do the api call in the click event. No need to update a state on click and the run an effect based on the updated state - that’s two levels of indirection. https://react.dev/learn/you-might-not-need-an-effect More on reddit.com
🌐 r/react
12
1
August 30, 2023
🌐
Coder
coder.earth › post › react-hooks-oops-part-3-an-effect-does-not-run-again-when-its-dependencies-change
React hooks... Oops! Part 3 - an effect doesn't run again when its dependencies change | Łukasz Makuch
function App() { // This fragment ... to it! 😲 · Why did it work before, but doesn't work now? It's because putting values in the dependency list doesn't turn them into any sort of observables. React won't be notified when they get mutated....
🌐
GitHub
github.com › facebook › react › issues › 24042
Bug: useEffect closure not called when dependencies changed · Issue #24042 · facebook/react
March 8, 2022 - I run into a very weird issue where the dependency of useEffect changes, but its closure is not called. The component renders correctly with the updated value due to a state change of a parent router, just the useEffect closure is not called.
Author   fabb
🌐
DEV Community
dev.to › cassidoo › when-useeffect-runs-3pf3
When useEffect runs - DEV Community
June 4, 2024 - Try using a ref in the dependency array and change it, the useEffect will NEVER run after that change because ref will never cause a re-render. Again, it's a small nuance, but it's important to get the difference to better understand the model.
🌐
DhiWise
dhiwise.com › post › how-to-fix-useeffect-not-triggering-in-your-react-project
Reasons Why UseEffect Is Not Triggering In Your React App
May 29, 2024 - This can be due to several reasons, such as an incorrect dependency array or the absence of dependencies that should trigger the effect. It’s essential to understand that useEffect runs after every render by default, but if you provide a ...
🌐
Medium
omarshishani.medium.com › why-useeffect-is-not-updating-when-props-change-c2e603a7e98b
Why useEffect is Not Updating When props Change 👻 | by Omar Shishani | Medium
January 9, 2021 - Why won’t my component register the changed values of props??? The answer for me was that I was not passing in props to useEffect as a dependency. It didn't even occur to me that it was useEffect which was causing the problem. In many cases, I use useEffect like componentDidMount.
Find elsewhere
🌐
Epic React
epicreact.dev › why-you-shouldnt-put-refs-in-a-dependency-array
Why you shouldn't put refs in a dependency array | Epic React by Kent C. Dodds
July 9, 2024 - Additionally (and consequentially), you should not expect any change in such values to result in the effect callback getting called. If you need the callback to be called when those things change, then you need to put it in useState (or useReducer).
🌐
React
react.dev › reference › react › useEffect
useEffect – React
However, you can express that you ... to changes even though it is called from inside an Effect. Declare an Effect Event with the useEffectEvent Hook, and move the code reading shoppingCart inside of it: ... Effect Events are not reactive and must always be omitted from dependencies of your Effect. This is what lets you put non-reactive code (where you can read the latest value of some props ...
🌐
GitHub
github.com › tannerlinsley › react-query › issues › 2628
Bug: useEffect not triggering on [deps] change · Issue #2628 · TanStack/query
September 2, 2021 - useEffect message is not visible upon clicking, even though [dependency] changed and UI is rerendered.
Author   Tosheen
🌐
DEV Community
dev.to › amitaldo › function-makes-the-dependencies-of-useeffect-hook-change-on-every-render-warning-in-react-35hd
"Function makes the dependencies of useEffect Hook change on every render" warning in React - DEV Community
March 7, 2023 - The ‘functionName’ function makes the dependencies of useEffect Hook (at line X) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of ‘functionName’ in its own useCallback() Hook.
🌐
Bitstack
blog.bitsrc.io › understanding-dependencies-in-useeffect-7afd4df37c96
Understanding Dependencies in useEffect | Bits and Pieces
March 4, 2025 - It must — or as we’ll see later, ... of these values changes (and after the initial render). The array of dependencies is not passed as argument to the effect function....
🌐
DhiWise
dhiwise.com › post › understanding-the-importance-of-the-useeffect-dependency-array-in-react
UseEffect Dependency Array: Best Practices For React ...
August 13, 2025 - Each update triggers a new render cycle, which runs the effect again, repeating forever. ... Make sure the effect updates state only when needed. Move non-changing variables out of the dependency array.
🌐
Kinsta®
kinsta.com › home › resource center › blog › react errors › how to fix the “react hook useeffect has a missing dependency” error
How To Fix the “React Hook useEffect Has a Missing ...
October 1, 2025 - This means when any of the values ... is re-executed. In a situation where a variable that the effect depends on is not included in the dependency array, the effect may not be re-executed when the value changes....
🌐
Medium
medium.com › @szaranger › how-to-fix-the-warning-react-hook-useeffect-has-missing-dependencies-288a6806fccf
How to fix the warning “React Hook useEffect has missing dependencies”? | by Sean Amarasinghe | Medium
December 16, 2024 - If you’re absolutely sure that the variables (initialPlans, weekdays, and weeks) won't change or it's okay to use their initial values, you can safely ignore the warning by disabling the ESLint rule temporarily within the effect: useEffect(() ...
🌐
TypeOfNaN
typeofnan.dev › fix-function-makes-the-dependencies-of-useeffect-hook-change-on-every-render-warning-in-react
Fix the "Function makes the dependencies of useEffect Hook change on every render" warning in React | TypeOfNaN
The solution when this is the case is to wrap the logCount function definition in a useCallback hook. What this does is returns a memoized function whose reference will only change if something in the hooks dependency array changes.
🌐
Bobby Hadz
bobbyhadz.com › blog › react-hook-useeffect-has-missing-dependency
React Hook useEffect has a missing dependency error [Fixed] | bobbyhadz
The hook will only recompute the memoized value if one of the dependencies has changed. Note that if you're working with a function, you would use the useCallback hook to get a memoized callback that doesn't change between renders.
🌐
Theodo
apps.theodo.com › en › article › how-to-avoid-bugs-in-useeffect
How to manage the useEffect dependency array like a pro?
March 1, 2022 - Let's add the missing dependency to our useEffect: Now it works! But re-render the app, create a new ++code>fetchAndStoreData++/code> function that triggers the useEffect, that sets a new state, that creates a new re-render, etc. We are in an infinite loop! This causes a lot of fetches, which is a problem for our backend, and a lot of re-renders, which is a problem for the user experience and performance. So, we change the dependency by ++code>postId++/code> because we don't want to memoize ++code>fetchAndStoreData,++/code> and it fixes all our problems:++code>++/code>
🌐
Codedamn
codedamn.com › news › react js
useEffect dependency array in React.js – Complete Guide
June 2, 2023 - These changes then trigger the callback function. The most basic dependency array would be an empty array. The empty array indicates that the useEffect doesn’t have any dependencies on any state variables. Therefore, the callback function is only called once the page renders in this case.
🌐
LogRocket
blog.logrocket.com › home › how to solve the react useeffect hook’s infinite loop patterns
How to solve the React useEffect Hook's infinite loop patterns - LogRocket Blog
June 4, 2024 - Eventually, this causes your application to crash · To solve this problem, we can make use of a useRefHook. This returns a mutable object which ensures that the reference does not ...