Redux 常见问题解答:一般的
¥Redux FAQ: General
我什么时候应该学习 Redux?
¥When should I learn Redux?
对于 JavaScript 开发者来说,学习什么可能是一个巨大的问题。通过一次学习一件事并专注于你在工作中发现的问题,有助于缩小选择范围。Redux 是一种管理应用状态的模式。如果你没有状态管理方面的问题,你可能会发现 Redux 的好处更难理解。一些 UI 库(如 React)有自己的状态管理系统。如果你正在使用这些库之一,特别是如果你刚刚学习使用它们,我们鼓励你首先学习该内置系统的功能。这可能是你构建应用所需的全部内容。如果你的应用变得如此复杂,以至于你对状态存储位置或状态如何更改感到困惑,那么现在是学习 Redux 的好时机。
¥What to learn can be an overwhelming question for a JavaScript developer. It helps to narrow the range of options by learning one thing at a time and focusing on problems you find in your work. Redux is a pattern for managing application state. If you do not have problems with state management, you might find the benefits of Redux harder to understand. Some UI libraries (like React) have their own state management system. If you are using one of these libraries, especially if you are just learning to use them, we encourage you to learn the capabilities of that built-in system first. It might be all you need to build your application. If your application becomes so complex that you are confused about where state is stored or how state changes, then it is a good time to learn Redux.
我们建议大多数新学习者应该首先专注于学习 React,等到你已经熟悉 React 后再学习 Redux。这样,一次需要学习的新概念就会更少,并且更清楚哪些概念是 React 的一部分,哪些概念是 Redux 的一部分。你还将更好地了解 Redux 如何适合 React 应用,以及 Redux 为何有用。
¥We recommend that most new learners should focus on learning React first, and wait to learn Redux until after you're already comfortable with React. That way, there's fewer new concepts to learn at once, and it's more clear what concepts are part of React and what concepts are part of Redux. You'll also have a better understanding of how using Redux fits into a React app, and why Redux can be useful.
更多信息
¥Further information
文章
¥Articles
讨论
¥Discussions
¥Ask HN: Overwhelmed with learning front-end, how do I proceed?
¥Twitter: If you want to teach someone to use an abstraction...
我什么时候应该使用 Redux?
¥When should I use Redux?
并非所有应用都需要 Redux。了解你正在构建的应用类型、需要解决的问题类型以及哪些工具可以最好地解决你面临的问题非常重要。
¥Not all apps need Redux. It's important to understand the kind of application you're building, the kinds of problems that you need to solve, and what tools can best solve the problems you're facing.
Redux 可帮助你处理共享状态管理,但与任何工具一样,它也有权衡。它并不是被设计为最短或最快的代码编写方式。它旨在通过可预测的行为帮助回答问题 "某个状态片何时发生变化,数据从何而来?"。有更多的概念需要学习,还有更多的代码需要编写。它还为你的代码添加了一些间接性,并要求你遵循某些限制。这是短期和长期生产力之间的权衡。
¥Redux helps you deal with shared state management, but like any tool, it has tradeoffs. It's not designed to be the shortest or fastest way to write code. It's intended to help answer the question "When did a certain slice of state change, and where did the data come from?", with predictable behavior. There are more concepts to learn, and more code to write. It also adds some indirection to your code, and asks you to follow certain restrictions. It's a trade-off between short term and long term productivity.
正如 React 早期贡献者之一 Pete Hunt 所说:
¥As Pete Hunt, one of the early contributors to React, says:
你会知道何时需要 Flux。如果你不确定是否需要它,那么你就不需要它。
¥You'll know when you need Flux. If you aren't sure if you need it, you don't need it.
同样,Redux 的创建者之一 Dan Abramov 表示:
¥Similarly, Dan Abramov, one of the creators of Redux, says:
我想修改一下:在使用普通 React 时遇到问题之前不要使用 Redux。
¥I would like to amend this: don't use Redux until you have problems with vanilla React.
Redux 在以下情况下最有用:
¥Redux is most useful in cases when:
你拥有应用中许多地方都需要的大量应用状态
¥You have large amounts of application state that are needed in many places in the app
应用状态经常更新
¥The app state is updated frequently
更新该状态的逻辑可能很复杂
¥The logic to update that state may be complex
该应用具有中型或大型代码库,并且可能由很多人使用
¥The app has a medium or large-sized codebase, and might be worked on by many people
你需要了解该状态如何随时间更新
¥You need to see how that state is being updated over time
还有许多其他可用工具可以帮助解决 Redux 所遇到的一些相同问题:状态管理、缓存获取的服务器数据以及通过 UI 传递数据。
¥There are also many other tools available that can help solve some of the same problems Redux does: state management, caching fetched server data, and passing data through the UI.
如果你不确定 Redux 是否适合你的应用,这些资源会提供更多指导:
¥If you're not sure whether Redux is a good choice for your app, these resources give some more guidance:
归根结底,Redux 只是一个工具。它是一个很棒的工具,有一些使用它的重要理由,但也有一些你可能不想使用它的原因。对你的工具做出明智的决策,并了解每个决策所涉及的权衡。
¥In the end, Redux is just a tool. It's a great tool, and there are some great reasons to use it, but there are also reasons you might not want to use it. Make informed decisions about your tools, and understand the tradeoffs involved in each decision.
更多信息
¥Further information
文档
¥Documentation
文章
¥Articles
讨论
¥Discussions
¥Twitter: Don't use Redux unless you're unhappy with local component state
红迪网:如果你的应用只是在单个页面上获取某些内容,则不需要 Redux
¥Reddit: You don't need Redux if your app just fetches something on a single page
¥Stack Overflow: What could be the downsides of using Redux instead of Flux?
Redux 只能与 React 一起使用吗?
¥Can Redux only be used with React?
Redux 可以用作任何 UI 层的数据存储。最常见的用法是 React 和 React Native,但也有可用于 Angular、Angular 2、Vue、Mithril 等的绑定。Redux 只是提供了一种可供任何其他代码使用的订阅机制。也就是说,当与声明性视图实现结合使用时,它是最有用的,该实现可以从状态更改推断 UI 更新,例如 React 或可用的类似库之一。
¥Redux can be used as a data store for any UI layer. The most common usage is with React and React Native, but there are bindings available for Angular, Angular 2, Vue, Mithril, and more. Redux simply provides a subscription mechanism which can be used by any other code. That said, it is most useful when combined with a declarative view implementation that can infer the UI updates from the state changes, such as React or one of the similar libraries available.
我需要有特定的构建工具才能使用 Redux 吗?
¥Do I need to have a particular build tool to use Redux?
Redux 使用现代 JS 语法(ES2020)编写,但代码相当简单。
¥Redux is written using modern JS syntax (ES2020), but the code is fairly simple.
如果你需要针对较旧的浏览器,请自行转译。
¥If you need to target older browsers, please transpile it yourself.
counter-vanilla 示例演示了基本的 ES5 用法,其中 Redux 作为 <script>
标签包含在内。正如相关的拉取请求所说:
¥The counter-vanilla example demonstrates basic ES5 usage with Redux included as a <script>
tag. As the relevant pull request says:
新的 Counter Vanilla 示例旨在消除这样的神话:Redux 需要 Webpack、React、热重载、saga、action 创建者、常量、Babel、npm、CSS 模块、装饰器、流利的拉丁语、Egghead 订阅、博士学位或超出预期的 O.W.L。等级。
¥The new Counter Vanilla example is aimed to dispel the myth that Redux requires Webpack, React, hot reloading, sagas, action creators, constants, Babel, npm, CSS modules, decorators, fluent Latin, an Egghead subscription, a PhD, or an Exceeds Expectations O.W.L. level.
不,它只是 HTML、一些手工
<script>
标签和普通的旧 DOM 操作。享受!¥Nope, it's just HTML, some artisanal
<script>
tags, and plain old DOM manipulation. Enjoy!