问题描述
为了了解 Redux 的动机,我阅读了官方链接 https://redux.js.org/introduction/motivation 和这个 stackoverflow 问题 解释 Redux:变异和异步性 但我我无法获得它,因为两个链接上都缺少示例.谷歌搜索也没有显示任何页面的突变和异步组合的例子.
To understand Redux motivation, I read the offical link https://redux.js.org/introduction/motivation and this stackoverflow question Explain Redux : mutation and asynchronicity but I am not able to get it as example is missing on both the links. Googling also not presented any page with example of combination of mutation and asynchronicity.
我想要的是现实生活中的 Web 应用程序示例,其中突变和异步性的组合是一团糟.每个页面/博客都以轻松管理状态开始,但它们缺少真实的 Web 应用程序示例,即变异和异步组合解释(动机).任何人都可以通过举个好例子来帮助我.
What I want is the real life web application example where combination of mutation and asynchronicity is mess. Every page/blog just starts with manage state easily but they are missing real web app example of mutation and asynchronicity combination explanation(motivation). Can anyone help me by giving good example.
动机是说:我们混合了两个非常困难的概念供人类思考:突变和异步.一世叫他们曼妥思和可乐.两者都可以很好地分离,但是他们一起造成了混乱..任何人都可以举出真实的例子吗?
推荐答案
使用异步和突变,你会失去管理状态的单一事实来源".
With both async and mutation, you lose your "single source of truth" for managing your state.
假设您有一个初始化为 0 的全局计数器.您还有 2 个带有异步处理程序的 Button 组件,inc 和 dec 会影响全局计数器.
Let's say you have a global counter initialized to 0. You also have 2 Button components with async handlers, inc and dec that affect the global counter.
如果我点击 inc, inc, dec;任何时候 counter 的值是多少?
If I click inc, inc, dec; What is the value of counter at any point?
我们无法知道哪些事件已经触发 b/c,它们不能保证按顺序进行.因此,如果我正在查看计数器值 1,是否为 b/c:
We have no way to know which event(s) already fired b/c they are not guaranteed to come in order. So if I'm looking at the counter value 1, is it b/c of:
已解决:inc
待定:inc, dec
pending: inc, dec
或
已解决:inc, inc, dec
resolved: inc, inc, dec
使用异步函数和可变状态,除非什么都没有发生,否则您永远不会真正知道自己的状态.
With async functions and mutatable state, you never really know what your state is unless nothing is happening.
这篇关于Redux 动机:突变和异步性示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!