Why not use cookies instead of Redux?

此生再无相见时 提交于 2019-12-02 12:27:49

问题


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.



来源:https://stackoverflow.com/questions/40848816/why-not-use-cookies-instead-of-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!