Why not use cookies instead of Redux?

2019-03-06 09:39发布

问题:

I have been reading up on Redux, and it solves a great number of problems. But in essence it is simply a central 'true' storage.

Intuitively though I find the fact that the state is still passed through props or context inelegant.

Aside from disk i/o speeds, why not use the local cookie store as a central data store? This would eliminate the need for passing the data along through components.

The only challenges I see is data safety, but that is not an issue for all applications.

Elaborating based on Dave's comments. My actual question is more about the possibility of having a central Redux style store without needing to actively pass along the state through props or context. Cookies seemed like an interesting first avenue to explore.

Edit: Thanks for the downvotes kind strangers. Good to know asking for help on a rarely discussed topic is punished by loving fellow programmers.

回答1:

Because you'd be endlessly retrieving cookie data and deciding if you need to re-render. This is no better than scattering data in the DOM or arbitrary web DB stores: the end problem is the same in that it's disconnected from rendering.

It completely removes one of React's benefits from your code: the whole point of having state and properties in React is that it handles re-rendering (or lack thereof) for you.

The Redux pattern is more than just having "central storage", which could be implemented in countless arbitrary ways. It also specifies how that storage is accessed (streams of actions) which provides a number of additional benefits. That's orthogonal to how the data is stored.

If you want more meaningful replies you'll need to defend your statement that "passing data through props or context is inelegant". It's one-way data flow; it's not inherently inelegant.