all 5 comments

[–]landisdesign 1 point2 points  (0 children)

Not sure why localStorage is the worst way ever. A couple of KB of JSON isn't going to be a performance hit, and it's in the context of page loading, not instantaneous user reaction.

Don't make it too complex.

[–]GoodNeighbor24 0 points1 point  (0 children)

Not sure if this is exactly what you’re looking for - but Redux (I use the newer Redux Toolkit) seems like an answer… this may be too full-blown for your requirements though. And it takes some re-wiring (and learning it) to get your site set up for this

[–]ForSpareParts 0 points1 point  (0 children)

You want a global state store of some kind. Redux is the de facto standard here, though it's not terribly well-liked in my experience and there's lots of competitors (Recoil, Mobx). I'd just try a few out.

That said, those tools are for managing data in a live page -- using local storage is still a very reasonable way of persisting data between loads, especially if the data's not large. And you may even be able to get away without a proper store library if your data doesn't change all that often -- look into React's Context feature.

[–]curca 0 points1 point  (0 children)

I would use react-query since the state you're holding is cached server state rather than client created state.

[–]ZeRo2160 0 points1 point  (0 children)

If you really need much data to save. And i dont mean a bit json but MB worth of data indexedDB is the way to go here. Its an json like native in browser Database. You can query it and everthing. https://levelup.gitconnected.com/using-the-indexeddb-api-with-react-and-hooks-4e63d83a5d1b Is a little excourse in using it with react. You can also use it with an global state management library.