UNPKG

14.1 kBMarkdownView Raw
1# [Redux](http://redux.js.org)
2
3Redux is a predictable state container for JavaScript apps.
4
5It 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](https://github.com/gaearon/redux-devtools).
6
7You can use Redux together with [React](https://facebook.github.io/react/), or with any other view library.
8It is tiny (2kB, including dependencies).
9
10[![build status](https://img.shields.io/travis/reactjs/redux/master.svg?style=flat-square)](https://travis-ci.org/reactjs/redux)
11[![npm version](https://img.shields.io/npm/v/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux)
12[![npm downloads](https://img.shields.io/npm/dm/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux)
13[![redux channel on discord](https://img.shields.io/badge/discord-%23redux%20%40%20reactiflux-61dafb.svg?style=flat-square)](https://discord.gg/0ZcbPKXt5bZ6au5t)
14[![#rackt on freenode](https://img.shields.io/badge/irc-%23rackt%20%40%20freenode-61DAFB.svg?style=flat-square)](https://webchat.freenode.net/)
15[![Changelog #187](https://img.shields.io/badge/changelog-%23187-lightgrey.svg?style=flat-square)](https://changelog.com/187)
16
17>**New! Learn Redux from its creator:
18>[Getting Started with Redux](https://egghead.io/series/getting-started-with-redux) (30 free videos)**
19
20### Testimonials
21
22>[“Love what you’re doing with Redux”](https://twitter.com/jingc/status/616608251463909376)
23>Jing Chen, creator of Flux
24
25>[“I asked for comments on Redux in FB's internal JS discussion group, and it was universally praised. Really awesome work.”](https://twitter.com/fisherwebdev/status/616286955693682688)
26>Bill Fisher, author of Flux documentation
27
28>[“It's cool that you are inventing a better Flux by not doing Flux at all.”](https://twitter.com/andrestaltz/status/616271392930201604)
29>André Staltz, creator of Cycle
30
31### Developer Experience
32
33I wrote Redux while working on my React Europe talk called [“Hot Reloading with Time Travel”](https://www.youtube.com/watch?v=xsSnOQynTHs). 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.
34
35### Influences
36
37Redux evolves the ideas of [Flux](http://facebook.github.io/flux/), but avoids its complexity by taking cues from [Elm](https://github.com/evancz/elm-architecture-tutorial/).
38Whether you have used them or not, Redux only takes a few minutes to get started with.
39
40### Installation
41
42To install the stable version:
43
44```
45npm install --save redux
46```
47
48This assumes you are using [npm](https://www.npmjs.com/) as your package manager.
49If you don’t, you can [access these files on npmcdn](https://npmcdn.com/redux/), download them, or point your package manager to them.
50
51Most commonly people consume Redux as a collection of [CommonJS](http://webpack.github.io/docs/commonjs.html) modules. These modules are what you get when you import `redux` in a [Webpack](http://webpack.github.io), [Browserify](http://browserify.org/), or a Node environment. If you like to live on the edge and use [Rollup](http://rollupjs.org), we support that as well.
52
53If you don’t use a module bundler, it’s also fine. The `redux` npm package includes precompiled production and development [UMD](https://github.com/umdjs/umd) builds in the [`dist` folder](https://npmcdn.com/redux/dist/). 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](https://npmcdn.com/redux/dist/redux.js) on the page, or [tell Bower to install it](https://github.com/reactjs/redux/pull/1181#issuecomment-167361975). The UMD builds make Redux available as a `window.Redux` global variable.
54
55The Redux source code is written in ES2015 but we precompile both CommonJS and UMD builds to ES5 so they work in [any modern browser](http://caniuse.com/#feat=es5). You don’t need to use Babel or a module bundler to [get started with Redux](https://github.com/reactjs/redux/blob/master/examples/counter-vanilla/index.html).
56
57#### Complementary Packages
58
59Most likely, you’ll also need [the React bindings](https://github.com/reactjs/react-redux) and [the developer tools](https://github.com/gaearon/redux-devtools).
60
61```
62npm install --save react-redux
63npm install --save-dev redux-devtools
64```
65
66Note that unlike Redux itself, many packages in the Redux ecosystem don’t provide UMD builds, so we recommend using CommonJS module bundlers like [Webpack](http://webpack.github.io) and [Browserify](http://browserify.org/) for the most comfortable development experience.
67
68### The Gist
69
70The whole state of your app is stored in an object tree inside a single *store*.
71The only way to change the state tree is to emit an *action*, an object describing what happened.
72To specify how the actions transform the state tree, you write pure *reducers*.
73
74That’s it!
75
76```js
77import { createStore } from 'redux'
78
79/**
80 * This is a reducer, a pure function with (state, action) => state signature.
81 * It describes how an action transforms the state into the next state.
82 *
83 * The shape of the state is up to you: it can be a primitive, an array, an object,
84 * or even an Immutable.js data structure. The only important part is that you should
85 * not mutate the state object, but return a new object if the state changes.
86 *
87 * In this example, we use a `switch` statement and strings, but you can use a helper that
88 * follows a different convention (such as function maps) if it makes sense for your
89 * project.
90 */
91function counter(state = 0, action) {
92 switch (action.type) {
93 case 'INCREMENT':
94 return state + 1
95 case 'DECREMENT':
96 return state - 1
97 default:
98 return state
99 }
100}
101
102// Create a Redux store holding the state of your app.
103// Its API is { subscribe, dispatch, getState }.
104let store = createStore(counter)
105
106// You can subscribe to the updates manually, or use bindings to your view layer.
107store.subscribe(() =>
108 console.log(store.getState())
109)
110
111// The only way to mutate the internal state is to dispatch an action.
112// The actions can be serialized, logged or stored and later replayed.
113store.dispatch({ type: 'INCREMENT' })
114// 1
115store.dispatch({ type: 'INCREMENT' })
116// 2
117store.dispatch({ type: 'DECREMENT' })
118// 1
119```
120
121Instead 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.
122
123If 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 there is just one root component in a React app, but it is composed out of many small components.
124
125This 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.
126
127### Learn Redux from Its Creator
128
129[Getting Started with Redux](https://egghead.io/series/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.**
130
131>[“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!”](https://twitter.com/sandrinodm/status/670548531422326785)
132>Sandrino Di Mattia
133
134>[“Plowing through @dan_abramov 'Getting Started with Redux' - its amazing how much simpler concepts get with video.”](https://twitter.com/chrisdhanaraj/status/670328025553219584)
135>Chris Dhanaraj
136
137>[“This video series on Redux by @dan_abramov on @eggheadio is spectacular!”](https://twitter.com/eddiezane/status/670333133242408960)
138>Eddie Zaneski
139
140>[“Come for the name hype. Stay for the rock solid fundamentals. (Thanks, and great job @dan_abramov and @eggheadio!)”](https://twitter.com/danott/status/669909126554607617)
141>Dan
142
143>[“This series of videos on Redux by @dan_abramov is repeatedly blowing my mind - gunna do some serious refactoring”](https://twitter.com/gelatindesign/status/669658358643892224)
144>Laurence Roberts
145
146So, what are you waiting for?
147
148#### [Watch the 30 Free Videos!](https://egghead.io/series/getting-started-with-redux)
149
150If you enjoyed my course, consider supporting Egghead by [buying a subscription](https://egghead.io/pricing). 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](https://egghead.io/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.
151
152### Documentation
153
154* [Introduction](http://redux.js.org/docs/introduction/index.html)
155* [Basics](http://redux.js.org/docs/basics/index.html)
156* [Advanced](http://redux.js.org/docs/advanced/index.html)
157* [Recipes](http://redux.js.org/docs/recipes/index.html)
158* [Troubleshooting](http://redux.js.org/docs/Troubleshooting.html)
159* [Glossary](http://redux.js.org/docs/Glossary.html)
160* [API Reference](http://redux.js.org/docs/api/index.html)
161
162For PDF, ePub, and MOBI exports for offline reading, and instructions on how to create them, please see: [paulkogel/redux-offline-docs](https://github.com/paulkogel/redux-offline-docs).
163
164### Examples
165
166* [Counter Vanilla](http://redux.js.org/docs/introduction/Examples.html#counter-vanilla) ([source](https://github.com/reactjs/redux/tree/master/examples/counter-vanilla))
167* [Counter](http://redux.js.org/docs/introduction/Examples.html#counter) ([source](https://github.com/reactjs/redux/tree/master/examples/counter))
168* [Todos](http://redux.js.org/docs/introduction/Examples.html#todos) ([source](https://github.com/reactjs/redux/tree/master/examples/todos))
169* [Todos with Undo](http://redux.js.org/docs/introduction/Examples.html#todos-with-undo) ([source](https://github.com/reactjs/redux/tree/master/examples/todos-with-undo))
170* [TodoMVC](http://redux.js.org/docs/introduction/Examples.html#todomvc) ([source](https://github.com/reactjs/redux/tree/master/examples/todomvc))
171* [Shopping Cart](http://redux.js.org/docs/introduction/Examples.html#shopping-cart) ([source](https://github.com/reactjs/redux/tree/master/examples/shopping-cart))
172* [Tree View](http://redux.js.org/docs/introduction/Examples.html#tree-view) ([source](https://github.com/reactjs/redux/tree/master/examples/tree-view))
173* [Async](http://redux.js.org/docs/introduction/Examples.html#async) ([source](https://github.com/reactjs/redux/tree/master/examples/async))
174* [Universal](http://redux.js.org/docs/introduction/Examples.html#universal) ([source](https://github.com/reactjs/redux/tree/master/examples/universal))
175* [Real World](http://redux.js.org/docs/introduction/Examples.html#real-world) ([source](https://github.com/reactjs/redux/tree/master/examples/real-world))
176
177If 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](https://github.com/jackielii/simplest-redux-example) that uses Redux together with React and Browserify.
178
179### Discussion
180
181Join the [#redux](https://discord.gg/0ZcbPKXt5bZ6au5t) channel of the [Reactiflux](http://www.reactiflux.com) Discord community.
182
183### Thanks
184
185* [The Elm Architecture](https://github.com/evancz/elm-architecture-tutorial) for a great intro to modeling state updates with reducers;
186* [Turning the database inside-out](http://www.confluent.io/blog/turning-the-database-inside-out-with-apache-samza/) for blowing my mind;
187* [Developing ClojureScript with Figwheel](https://www.youtube.com/watch?v=j-kj2qwJa_E) for convincing me that re-evaluation should “just work”;
188* [Webpack](https://github.com/webpack/docs/wiki/hot-module-replacement-with-webpack) for Hot Module Replacement;
189* [Flummox](https://github.com/acdlite/flummox) for teaching me to approach Flux without boilerplate or singletons;
190* [disto](https://github.com/threepointone/disto) for a proof of concept of hot reloadable Stores;
191* [NuclearJS](https://github.com/optimizely/nuclear-js) for proving this architecture can be performant;
192* [Om](https://github.com/omcljs/om) for popularizing the idea of a single state atom;
193* [Cycle](https://github.com/cyclejs/cycle-core) for showing how often a function is the best tool;
194* [React](https://github.com/facebook/react) for the pragmatic innovation.
195
196Special thanks to [Jamie Paton](http://jdpaton.github.io) for handing over the `redux` NPM package name.
197
198### Change Log
199
200This project adheres to [Semantic Versioning](http://semver.org/).
201Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/reactjs/redux/releases) page.
202
203### Patrons
204
205The work on Redux was [funded by the community](https://www.patreon.com/reactdx).
206Meet some of the outstanding companies that made it possible:
207
208* [Webflow](https://github.com/webflow)
209* [Ximedes](https://www.ximedes.com/)
210
211[See the full list of Redux patrons.](PATRONS.md)
212
213### License
214
215MIT