Hooks: reducer + useEffect
See original GitHub issueUsing the example at https://reactjs.org/docs/hooks-reference.html#usereducer when I try to add
useEffect( () => {
console.log('state changed')
}, [state])
And if the code ends up calling action.type
to the default-action (which is return the previous state), the above effect
is still executed. Is this a desired behavior?
Do you want to request a feature or report a bug? Possibly a bug?
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
What is the expected behavior?
When a reducer retains the previous state and the state is used in useEffect
, it should not trigger this as state change.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 16.7.0-alpha.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
I couldn’t reproduce this.
When pressing the ‘default’ button, the useEffect never runs. When pressing the ‘reset’ button, the useEffect always runs if that wasn’t the latest button clicked.
This is behaving exactly as intended, I believe.
This needs a reproducing example.