question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`useReducer` does not honor reducer function's default state value

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
gaearoncommented, Feb 8, 2019

I think useReducer(reducer, undefined, { type: 'INIT' }) is what we suggest so that strong typing can work.

Edit: final API has changed, see below

3reactions
ckknightcommented, Jan 9, 2019

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. Because initialState is provided as undefined, it will appropriately default to 5 as specified in the reducer.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found