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.

wrapRootElement + contextAPI : Page changes cause provider to re-render therefore resetting state.

See original GitHub issue

Summary

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:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
dagosscommented, Mar 25, 2020

@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.

0reactions
PaulBunkercommented, Mar 10, 2020

I fixed this problem using the 3rd argument for useReducer() that is used for initialising the state

Read more comments on GitHub >

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

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