Next.js Discord

Discord Forum

Reset context values on navigation

Unanswered
Tomistoma posted this in #help-forum
Open in Discord
TomistomaOP
My root page, /, contains an OpenLayers map. The map object itself, as well as other information about the map, such as config, layers, etc, are stored in a context (MapContext) that the map component (MapComponent), and other components use.

What I am noticing is that the value of this context is preserved between page navigations, for example, if I start at / so the map is loaded and initialised, then navigate to /foo, and then back to /, the previous values of the MapContext remain the same. This is causing issues with the map as the element the map is mounted to get destroyed, therefore the map is no longer visible, but it also won't be recreated since it still exists in the context.
My ideal solution is that the context is reset every time you navigate away from /, so that an entirely new map is created every time I load /.

Currently, the MapContext provider is wrapping the app in the root layout.tsx. I have also tried moving the / page into a route group and adding its own layout that contains the context instead, so I am certain the context is being umounted when navigating away, however that hasn't fixed it.

Is resetting the context on navigation possible?

0 Replies