James B
James B

Reputation: 452

Does React's Context handle browser back button?

In my application I have a context for search information. I can do new searches, clear the searches, etc -- and everything works okay.

The issue is, when I click the browser's back button, my context doesn't update with the old data.

Previously I used history.push and pushed a state object, which I picked up in all my components. After having switched all of that to use a context, I'm having trouble.


The code is set up a bit like this:

A definition for my state:

and a return statement containing the following:

<SearchContext.Provider value={[searchData, setSearchData]}
   ...
</SearchContext.Provider>

Now I can consume this and pull this context in everywhere I need it with useContext. I can perform a search which will setSearchData, and any console.logs in child components are accurate -- I can clear the search and perform new searches, and the information is still logged accurately. But when I click the back button, what's in the context, and what's logged, doesn't represent the previous data.

Am I completely misunderstanding the capabilities of "vanilla" context? Do I need to use history.push? What is the "right" way to do this?

Upvotes: 0

Views: 782

Answers (0)

Related Questions