wrapRootElement + contextAPI : Page changes cause provider to re-render therefore resetting state.
See original GitHub issueSummary
I am using React context api and hooks to create a global ‘StateProvider’ however when I change pages my provider re-renders causing me to loose the active state and return to the default.
It is my understanding that using the wrapRootElement method should mean that my stateProvider should persist.
Some advice on how I can debug this issue would be greatly appreciated!
Relevant information
gatsby-browser.js
const StateProvider = require("./src/components/store/store").StateProvider
exports.wrapRootElement = ({ element }) => {
return <StateProvider>{element}</StateProvider>
}
store.js
const StateProviderContext = createContext()
const DispatchProviderContext = createContext()
const StateProvider = ({ children }) => {
console.log("init")
const tocTree = useEntries()
const initialState = reducer(
{},
{
type: INIT_PAGE,
value: { pathname, tocTree },
}
)
const [state, dispatch] = useReducer(reducer, initialState)
return (
<StateProviderContext.Provider value={state}>
<DispatchProviderContext.Provider value={dispatch}>
{children}
</DispatchProviderContext.Provider>
</StateProviderContext.Provider>
)
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
Page changes cause provider to re-render therefore resetting ...
I am using React context api and hooks to create a global 'StateProvider' however when I change pages my provider re-renders causing me...
Read more >How did I re-render: Sharing State through React-Context
Another concern with a “lifted state” is to pass the state down the component tree recursively through component props. (Commonly known as 'prop-drilling')....
Read more >How to use React's Context API with Gatsby - Paul Scanlon
Hey there, in this post I'll be explaining how to use React's Context API with Gatsby to provide a "global state" variable and...
Read more >How YOU can use the Context API in React.js to pubsub
Now it's easy for us to change the state and thereby we can change the value the Provider is providing to any Consumer...
Read more >React State management - Part 2 - Saeloun Blog
Accessing a DOM element. Storing mutable information without triggering a re-render of that component. A ref provides a way to access DOM nodes ......
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
@PaulBunker I’m also running into this issue. Can you clarify by what you did to solve this?
Edit: Nevermind. I found this blog post that explains things.
I fixed this problem using the 3rd argument for useReducer() that is used for initialising the state