Redux不只是美化了全球状态吗? [英] Isn't Redux just glorified global state?

查看:99
本文介绍了Redux不只是美化了全球状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一周前开始学习React,我不可避免地遇到了状态问题以及组件应该如何与应用程序的其余部分进行通信.我四处搜寻,Redux似乎是本月的风土人情.我通读了所有文档,我认为这实际上是一个非常革命性的想法.这是我的想法:

So I started learning React a week ago and I inevitably got to the problem of state and how components are supposed to communicate with the rest of the app. I searched around and Redux seems to be the flavor of the month. I read through all the documentation and I think it's actually a pretty revolutionary idea. Here are my thoughts on it:

状态通常被认为是非常邪恶的,并且是程序设计中大量错误的来源. Redux并未将所有内容分散在整个应用程序中,而是为什么不将其全部集中在必须发出动作才能更改的全局状态树中?听起来不错.所有程序都需要状态,因此让我们将其放在一个不纯净的空间中,并仅从那里进行修改,以便轻松查找错误.然后,我们还可以声明性地将各个状态块绑定到React组件,并使它们自动重绘,并且一切都很好.

State is generally agreed to be pretty evil and a large source of bugs in programming. Instead of scattering it all throughout your app Redux says why not just have it all concentrated in a global state tree that you have to emit actions to change? Sounds interesting. All programs need state so let's stick it in one impure space and only modify it from within there so bugs are easy to track down. Then we can also declaratively bind individual state pieces to React components and have them auto-redraw and everything is beautiful.

但是,关于整个设计,我有两个问题.首先,为什么状态树必须是不可变的?假设我不在乎时间旅行调试,热重装,并且已经在我的应用程序中实现了撤消/重做.不得不这样做似乎很麻烦:

However, I have two questions about this whole design. For one, why does the state tree need to be immutable? Say I don't care about time travel debugging, hot reload, and have already implemented undo/redo in my app. It just seems so cumbersome to have to do this:

case COMPLETE_TODO:
  return [
    ...state.slice(0, action.index),
    Object.assign({}, state[action.index], {
      completed: true
    }),
    ...state.slice(action.index + 1)
  ];

代替此:

case COMPLETE_TODO:
  state[action.index].completed = true;

更不用说我正在制作一个在线白板来学习,并且每个状态更改都可能像在命令列表中添加笔刷一样简单.过了一段时间(数百个笔触),复制整个阵列可能开始变得非常昂贵和费时.

Not to mention I am making an online whiteboard just to learn and every state change might be as simple as adding a brush stroke to the command list. After a while (hundreds of brush strokes) duplicating this entire array might start becoming extremely expensive and time-consuming.

我可以接受一个全局状态树,该状态树与通过操作进行更改的UI无关,但是它真的需要不可变吗?像这样的简单实现有什么问题(草稿很粗,写在1分钟之内)?

I'm ok with a global state tree that is independent from the UI that is mutated via actions, but does it really need to be immutable? What's wrong with a simple implementation like this (very rough draft. wrote in 1 minute)?

var store = { items: [] };

export function getState() {
  return store;
}

export function addTodo(text) {
  store.items.push({ "text": text, "completed", false});
}

export function completeTodo(index) {
  store.items[index].completed = true;
}

它仍然是通过发出的动作而突变的全局状态树,但是极其简单和有效.

It's still a global state tree mutated via actions emitted but extremely simple and efficient.

推荐答案

Redux不只是美化了全球状态吗?

Isn't Redux just glorified global state?

当然可以.但是,您曾经使用过的每个数据库都适用.最好将Redux视为一个内存数据库-您的组件可以依赖于该数据库.

Of course it is. But the same holds for every database you have ever used. It is better to treat Redux as an in-memory database - which your components can reactively depend upon.

不可变性使您可以非常有效地检查子树是否被更改,因为它简化了身份检查.

Immutability enables checking if any sub-tree has been altered very efficient because it simplifies down to an identity check.

是的,您的实现是有效的,但是每次以某种方式操纵树时,都必须重新渲染整个虚拟dom.

Yes, your implementation is efficient, but the entire virtual dom will have to be re-rendered each time the tree is manipulated somehow.

如果您使用React,它将最终与实际的dom进行对比,并执行最少的批次优化操作,但是完整的自上而下的重新渲染仍然效率低下.

If you are using React, it will eventually do a diff against the actual dom and perform minimal batch-optimized manipulations, but the full top-down re-rendering is still inefficient.

对于一棵不可变的树,无状态组件只需检查它所依赖的子树,与先前值相比的身份是否不同,如果是,则可以完全避免渲染.

For an immutable tree, stateless components just have to check if the subtree(s) it depends on, differ in identities compared to previous value(s), and if so - the rendering can be avoided entirely.

这篇关于Redux不只是美化了全球状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆