@0xcda7a/redux-es6 中文文档教程

发布于 7年前 浏览 27 项目主页 更新于 3年前

Redux 是 JavaScript 应用程序的可预测状态容器。
(如果您正在寻找 WordPress 框架,请查看 Redux Framework。)

它可以帮助您编写行为一致、在不同环境中运行的应用程序(客户端、服务器和本机),并且易于测试。 最重要的是,它提供了出色的开发人员体验,例如实时代码编辑与时间旅行调试器相结合

您可以将 Redux 与 React 或任何其他视图库一起使用。
它很小(2kB,包括依赖项)。

构建状态npm 版本npm 下载redux channel on discord#rackt on freenodeChangelog #187

向其创建者学习 Redux:
第 1 部分:Redux 入门(30 个免费视频)
第 2 部分:使用 Idiomatic Redux 构建 React 应用程序(27 个免费视频)< /strong>

Testimonials

“喜欢你用 Redux 做的事”
Flux 的创始人 Jing Chen

“我在 FB 的内部 JS 讨论群中征求对 Redux 的意见,得到了普遍好评。 真的很棒。”
Bill Fisher,Flux 文档的作者

“很酷,你通过根本不做 Flux 而发明了更好的 Flux。” br> André Staltz,Cycle 的创造者

Before Proceeding Further

另请阅读:
你可能不需要 Redux

Developer Experience

我在工作的时候写了 Redux在我名为 “带时间旅行的热重载” 的 React Europe 演讲中。 我的目标是创建一个具有最少 API 但行为完全可预测的状态管理库,因此可以实现日志记录、热重载、时间旅行、通用应用程序、记录和重播,而无需开发人员的任何支持。

Influences

Redux 改进了 Flux 的思想,但通过从 榆树
无论您是否使用过它们,Redux 只需几分钟即可上手。

Installation

安装稳定版:

npm install --save redux

假设您使用 npm 作为包管理器。

如果你不是,你可以在 unpkg 上访问这些文件,下载它们,或者将你的包管理器指向它们。

大多数情况下,人们将 Redux 作为 CommonJS 模块的集合来使用。 这些模块是你在 Webpack 中导入 redux 时得到的,Browserify,或 Node 环境。 如果你喜欢生活在边缘并使用 Rollup,我们也支持。

如果您不使用模块捆绑器,也可以。 redux npm 包包括预编译的生产和开发 UMD 构建在 dist 文件夹。 它们可以在没有捆绑器的情况下直接使用,因此与许多流行的 JavaScript 模块加载器和环境兼容。 例如,您可以将 UMD 构建作为

Redux 源代码是用 ES2015 编写的,但我们将 CommonJS 和 UMD 构建预编译为 ES5,因此它们可以在任何现代浏览器中工作。 你不需要使用 Babel 或模块打包器来开始使用 Redux< /a>。

Complementary Packages

很可能,您还需要 React 绑定开发者工具

npm install --save react-redux
npm install --save-dev redux-devtools

请注意,与 Redux 本身不同,Redux 生态系统中的许多包不提供 UMD 构建,因此我们建议使用 CommonJS 模块打包器,例如 WebpackBrowserify 以获得最舒适的开发体验。

The Gist

您的应用程序的整个状态存储在单个商店内的对象树中。
改变状态树的唯一方法是发出一个动作,一个描述发生了什么的对象。
要指定操作如何转换状态树,您可以编写纯 reducers

就是这样!

import { createStore } from 'redux'

/**
 * This is a reducer, a pure function with (state, action) => state signature.
 * It describes how an action transforms the state into the next state.
 *
 * The shape of the state is up to you: it can be a primitive, an array, an object,
 * or even an Immutable.js data structure. The only important part is that you should
 * not mutate the state object, but return a new object if the state changes.
 *
 * In this example, we use a `switch` statement and strings, but you can use a helper that
 * follows a different convention (such as function maps) if it makes sense for your
 * project.
 */
function counter(state = 0, action) {
  switch (action.type) {
  case 'INCREMENT':
    return state + 1
  case 'DECREMENT':
    return state - 1
  default:
    return state
  }
}

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(counter)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// However it can also be handy to persist the current state in the localStorage.

store.subscribe(() =>
  console.log(store.getState())
)

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1

您不是直接改变状态,而是使用称为 actions 的普通对象指定希望发生的改变。 然后您编写一个称为 reducer 的特殊函数来决定每个操作如何转换整个应用程序的状态。

如果您来自 Flux,则需要了解一个重要的区别。 Redux 没有 Dispatcher 也不支持很多商店。 相反,只有一个具有单个根减少功能的商店。 随着应用程序的增长,您无需添加商店,而是将根 reducer 拆分为更小的 reducer,独立地在状态树的不同部分上运行。 这就像 React 应用程序中只有一个根组件,但它由许多小组件组成。

这种架构对于计数器应用程序来说似乎有点矫枉过正,但这种模式的美妙之处在于它可以很好地扩展到大型和复杂的应用程序。 它还支持非常强大的开发人员工具,因为可以将每个突变跟踪到导致它的操作。 您可以记录用户会话并通过重放每个操作来重现它们。

Learn Redux from Its Creator

Redux 入门 是由 Redux 的作者 Dan Abramov 讲述的一个视频课程,包含 30 个视频。 它旨在补充文档的“基础”部分,同时带来关于不变性、测试、Redux 最佳实践以及将 Redux 与 React 结合使用的更多见解。 这门课程是免费的,而且永远免费。

“@dan_abramov 在 egghead.io 上开设的很棒的课程 - 而不仅仅是向您展示如何使用 #redux,它还展示了 redux 的构建方式和原因!”
Sandrino Di Mattia

“仔细阅读@dan_abramov 'Redux 入门' - 令人惊讶的是,视频中的概念变得如此简单。”
Chris Dhanaraj

“@dan_abramov 在@eggheadio 上制作的关于 Redux 的视频系列非常精彩!”
Eddie Zaneski

“来炒作这个名字。 坚守坚如磐石的基础。 (谢谢,@dan_abramov 和@eggheadio 干得好!)”
Dan

“@dan_abramov 的这一系列关于 Redux 的视频一再让我震惊 - gunna 做一些认真的重构”
劳伦斯·罗伯茨

那么,您还在等什么?

Watch the 30 Free Videos!

如果您喜欢我的课程,请考虑通过购买订阅 来支持 Egghead。 订阅者可以访问我每个视频中示例的源代码,以及其他主题的大量高级课程,包括深入 JavaScript、React、Angular 等。 许多 Egghead 讲师 也是开源库作者,因此购买订阅是感谢他们所做工作的好方法。

Documentation

对于离线阅读的 PDF、ePub 和 MOBI 导出,以及如何创建它们的说明,请参阅:paulkogel/redux-offline -文档

离线文档请看:devdocs

Examples

几乎所有的例子都有对应的CodeSandbox沙箱。 这是您可以在线玩的代码的交互式版本。

如果您是 NPM 生态系统的新手并且在启动和运行项目时遇到困难,或者不确定将上面的要点粘贴到哪里,请查看 simplest-redux-example 将 Redux 与 React 和 Browserify 结合使用。

Discussion

加入 Reactiflux Discord 社区的#redux 频道.

Thanks

特别感谢 Jamie Paton 交出 redux NPM 包名。

您可以在 GitHub 上找到官方徽标。

Change Log

该项目遵循语义版本控制
每个版本以及迁移说明都记录在 Github Releases 页面上。

Patrons

Redux 的工作由社区资助
认识一些使之成为可能的杰出公司:

查看 Redux 赞助人的完整列表。,以及不断增长的 使用 Redux 的人和公司

License

麻省理工学院

Redux is a predictable state container for JavaScript apps.
(If you're looking for a WordPress framework, check out Redux Framework.)

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

You can use Redux together with React, or with any other view library.
It is tiny (2kB, including dependencies).

build statusnpm versionnpm downloadsredux channel on discord#rackt on freenodeChangelog #187

Learn Redux from its creator:
Part 1: Getting Started with Redux (30 free videos)
Part 2: Building React Applications with Idiomatic Redux (27 free videos)

Testimonials

“Love what you're doing with Redux”
Jing Chen, creator of Flux

“I asked for comments on Redux in FB's internal JS discussion group, and it was universally praised. Really awesome work.”
Bill Fisher, author of Flux documentation

“It's cool that you are inventing a better Flux by not doing Flux at all.”
André Staltz, creator of Cycle

Before Proceeding Further

Also read:
You Might Not Need Redux

Developer Experience

I wrote Redux while working on my React Europe talk called “Hot Reloading with Time Travel”. My goal was to create a state management library with minimal API but completely predictable behavior, so it is possible to implement logging, hot reloading, time travel, universal apps, record and replay, without any buy-in from the developer.

Influences

Redux evolves the ideas of Flux, but avoids its complexity by taking cues from Elm.
Whether you have used them or not, Redux only takes a few minutes to get started with.

Installation

To install the stable version:

npm install --save redux

This assumes you are using npm as your package manager.

If you're not, you can access these files on unpkg, download them, or point your package manager to them.

Most commonly people consume Redux as a collection of CommonJS modules. These modules are what you get when you import redux in a Webpack, Browserify, or a Node environment. If you like to live on the edge and use Rollup, we support that as well.

If you don't use a module bundler, it's also fine. The redux npm package includes precompiled production and development UMD builds in the dist folder. They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. For example, you can drop a UMD build as a <script> tag on the page, or tell Bower to install it. The UMD builds make Redux available as a window.Redux global variable.

The Redux source code is written in ES2015 but we precompile both CommonJS and UMD builds to ES5 so they work in any modern browser. You don't need to use Babel or a module bundler to get started with Redux.

Complementary Packages

Most likely, you'll also need the React bindings and the developer tools.

npm install --save react-redux
npm install --save-dev redux-devtools

Note that unlike Redux itself, many packages in the Redux ecosystem don't provide UMD builds, so we recommend using CommonJS module bundlers like Webpack and Browserify for the most comfortable development experience.

The Gist

The whole state of your app is stored in an object tree inside a single store.
The only way to change the state tree is to emit an action, an object describing what happened.
To specify how the actions transform the state tree, you write pure reducers.

That's it!

import { createStore } from 'redux'

/**
 * This is a reducer, a pure function with (state, action) => state signature.
 * It describes how an action transforms the state into the next state.
 *
 * The shape of the state is up to you: it can be a primitive, an array, an object,
 * or even an Immutable.js data structure. The only important part is that you should
 * not mutate the state object, but return a new object if the state changes.
 *
 * In this example, we use a `switch` statement and strings, but you can use a helper that
 * follows a different convention (such as function maps) if it makes sense for your
 * project.
 */
function counter(state = 0, action) {
  switch (action.type) {
  case 'INCREMENT':
    return state + 1
  case 'DECREMENT':
    return state - 1
  default:
    return state
  }
}

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(counter)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// However it can also be handy to persist the current state in the localStorage.

store.subscribe(() =>
  console.log(store.getState())
)

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1

Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called actions. Then you write a special function called a reducer to decide how every action transforms the entire application's state.

If you're coming from Flux, there is a single important difference you need to understand. Redux doesn't have a Dispatcher or support many stores. Instead, there is just a single store with a single root reducing function. As your app grows, instead of adding stores, you split the root reducer into smaller reducers independently operating on the different parts of the state tree. This is exactly like how there is just one root component in a React app, but it is composed out of many small components.

This architecture might seem like an overkill for a counter app, but the beauty of this pattern is how well it scales to large and complex apps. It also enables very powerful developer tools, because it is possible to trace every mutation to the action that caused it. You can record user sessions and reproduce them just by replaying every action.

Learn Redux from Its Creator

Getting Started with Redux is a video course consisting of 30 videos narrated by Dan Abramov, author of Redux. It is designed to complement the “Basics” part of the docs while bringing additional insights about immutability, testing, Redux best practices, and using Redux with React. This course is free and will always be.

“Great course on egghead.io by @dan_abramov - instead of just showing you how to use #redux, it also shows how and why redux was built!”
Sandrino Di Mattia

“Plowing through @dan_abramov 'Getting Started with Redux' - its amazing how much simpler concepts get with video.”
Chris Dhanaraj

“This video series on Redux by @dan_abramov on @eggheadio is spectacular!”
Eddie Zaneski

“Come for the name hype. Stay for the rock solid fundamentals. (Thanks, and great job @dan_abramov and @eggheadio!)”
Dan

“This series of videos on Redux by @dan_abramov is repeatedly blowing my mind - gunna do some serious refactoring”
Laurence Roberts

So, what are you waiting for?

Watch the 30 Free Videos!

If you enjoyed my course, consider supporting Egghead by buying a subscription. Subscribers have access to the source code for the example in every one of my videos, as well as to tons of advanced lessons on other topics, including JavaScript in depth, React, Angular, and more. Many Egghead instructors are also open source library authors, so buying a subscription is a nice way to thank them for the work that they've done.

Documentation

For PDF, ePub, and MOBI exports for offline reading, and instructions on how to create them, please see: paulkogel/redux-offline-docs.

For Offline docs, please see: devdocs

Examples

Almost all examples have a corresponding CodeSandbox sandbox. This is an interactive version of the code that you can play with online.

If you're new to the NPM ecosystem and have troubles getting a project up and running, or aren't sure where to paste the gist above, check out simplest-redux-example that uses Redux together with React and Browserify.

Discussion

Join the #redux channel of the Reactiflux Discord community.

Thanks

Special thanks to Jamie Paton for handing over the redux NPM package name.

You can find the official logo on GitHub.

Change Log

This project adheres to Semantic Versioning.
Every release, along with the migration instructions, is documented on the Github Releases page.

Patrons

The work on Redux was funded by the community.
Meet some of the outstanding companies that made it possible:

See the full list of Redux patrons., as well as the always-growing list of people and companies that use Redux.

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文