Context is not keeping the data and is resetting the state when I change pages
Answered
King Charles Spaniel posted this in #help-forum
King Charles SpanielOP
I created some contexts to deal with data and placed the providers inside Layout.tsx which is inside the App folder, and I have 3 pages, the main page in the App folder, a page inside [id] and another page inside [ id]/details/[featureId], on the page within [id] I set some things within the context and when I go to the page within [featureId] , and try to use the data that was set, it is null, how can I resolve this to let the context remain?
8 Replies
Plott Hound
the data in your context will be retained unless you hard refresh a page or use something like a redirect internally. are you using
Link for internal navigation?@Plott Hound the data in your context will be retained unless you hard refresh a page or use something like a redirect internally. are you using `Link` for internal navigation?
King Charles SpanielOP
No i used href to navigate in this case
@King Charles Spaniel No i used href to navigate in this case
Plott Hound
Try navigating with the Link component and it should retain your context
@Plott Hound Try navigating with the Link component and it should retain your context
King Charles SpanielOP
Using router.replace can work as well?
@King Charles Spaniel Using router.replace can work as well?
Plott Hound
No I think it will clear the context
Answer
Plott Hound
If for some reason you can’t use Link you’d have to use different state management or persist your context values in local storage
router.replace() should do soft navigation too but its better to use <Link /> which render a <a /> tag. So it will still work even javascript is not enabledKing Charles SpanielOP
Ok thank you guys