codewithmohsen / learn-from-usecontext-to-usereducer

Professional learning useContext in the easiest possible way for those who have tried any other ways.
0 stars 0 forks source link

Why provider state updated but reset in new tab #1

Open codewithmohsen opened 1 year ago

codewithmohsen commented 1 year ago

Solution: This is expected, the context API is only meant to help you not have to pass properties down each element in a component tree. It does nothing for persistent state management. You'll need to add some code that both stores the state into something like localStorage and also loads it from localStorage on page load. See some code examples here: https://medium.com/@akrush95/global-cached-state-in-react-using-hooks-context-and-local-storage-166eacf8ab46

src: https://stackoverflow.com/questions/61580639/contextapi-loses-state-on-page-reload-and-when-opened-in-new-tab

codewithmohsen commented 1 year ago

https://stackoverflow.com/questions/71451505/use-react-context-with-local-storage

codewithmohsen commented 1 year ago

now

https://stackoverflow.com/questions/71451505/use-react-context-with-local-storage

now it save and load from local storage and when i open new tabs it still saved! but in realtime other page's stare provider not change immidiately when localstorage changd.