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.

Pass initial state to useReducer

See original GitHub issue

First and foremost, awesome idea with using Proxy to track conditional component updates.

How would I go about passing a dynamic initial state into useReducer? Something like the following:

const useValue = value => useReducer(store.reducer, value);

export function StoreProvider({ value, children }) {
    return <Provider useValue={() => useValue(value)}>{children}</Provider>;
}

In the above case I get:

useValue must be statically defined

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Aug 21, 2019

TypeError: useValue is not a function

My bad. It should be:

const useValue = useMemo(() => () => useReducer(store.reducer, value), []);
return <Provider useValue={useValue}>{children}</Provider>;
0reactions
dai-shicommented, Aug 21, 2019

Thanks. I will file a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React useReducer Hook ultimate guide - LogRocket Blog
Specifying the initial state ... If you don't pass a third argument to useReducer , it will take the second argument as the...
Read more >
how to set the initialState with useReducer React
You can just call the searchUsers() function in useEffect() to get some users and set the state. if you want to get initial...
Read more >
An Easy Guide to React useReducer() Hook - Dmitri Pavlutin
The useReducer(reducer, initialState) hook accept 2 arguments: ... Start, Stop and Reset, and has a number displaying the passed seconds.
Read more >
3 Managing component state with the useReducer hook
The component passes the initial state and the reducer to the useReducer function. React sets the current state for the reducer as the...
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 >

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