`useReducer` does not honor reducer function's default state value
See original GitHub issueDo you want to request a feature or report a bug?
Bug
What is the current behavior?
Please see the CodeSandBox here: https://codesandbox.io/s/v8y72ky0o5
Consider the following reducer:
function reducer(state = 5, { type }) {
switch (type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
Suppose we are trying to use the reducer in a component like so:
function Counter() {
const [count] = useReducer(reducer)
return <span>{count}</span>
}
When rendered, count
is undefined
.
What is the expected behavior?
One would expect count
to be equal to 5
, because that is the default value for state in the reducer.
IMO, it would make sense if initialState
argument for useReducer
is undefined, to call reducer(undefined, undefined)
to initialize the internal state.
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.2
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Problem with reducer function in useReducer hook
Yes, the userReducer hook is in the parent component, and there is a child component that calls the addItemHandler .As far as I...
Read more >React useReducer Hook ultimate guide - LogRocket Blog
useReducer returns an array that holds the current state value and a dispatch function to which you can pass an action and later...
Read more >How to use React useReducer hook like a pro - Devtrium
The first is the state , and the second is a function that lets you modify the state: setState for useState , and...
Read more >Solved The Redux framework and React's useReducer hook
Question: The Redux framework and React's useReducer hook both require you to define a type of function called a “Reducer”. Below is an...
Read more >An Easy Guide to React useReducer() Hook - Dmitri Pavlutin
The initial state is the value the state is initialized with. ... The dispatch function is created for you by the useReducer() hook:....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think
useReducer(reducer, undefined, { type: 'INIT' })
is what we suggest so that strong typing can work.Edit: final API has changed, see below
useReducer(reducer, undefined, { type: 'INIT' })
will work as you expect. It will execute the reducer initially, once, with a{ type: 'INIT' }
action, thus falling through to the default case. BecauseinitialState
is provided asundefined
, it will appropriately default to5
as specified in the reducer.