-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Docs improvements #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Docs improvements #140
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
ad49ffa
Add terminology.md
acdlite 22f377b
Start FAQ document
emmenko 6c5f055
Merge branch 'master' into improve-docs
emmenko d96f5f4
Add some JSDoc
emmenko af89752
More JSDoc
emmenko da2fa45
More JSDoc
emmenko 39a8107
Still more JSDoc
emmenko 0c16baa
Merge pull request #214 from gaearon/gaearon-patch-1
gaearon b15f0e6
Add terminology.md
acdlite 41f0bdf
Start FAQ document
emmenko d816847
Add some JSDoc
emmenko b8308ec
More JSDoc
emmenko 9de7b44
More JSDoc
emmenko bbca371
Still more JSDoc
emmenko 35b8da5
Merge branch 'improve-docs' of https://github.com/gaearon/redux into …
acdlite 5efcc08
Add higher-order store docs
acdlite c7e37e4
Split README into separate docs
acdlite 01b8e41
FAQ
acdlite 9522aed
Add types and terminology
acdlite 1f19428
Remove extraneous file
acdlite 9de7445
Typos and clarficiation
acdlite 7e5a829
Fix higher-order store type signature
acdlite 5433e2c
Fix Flow types
acdlite d64f1b4
Update store.md
ellbee 4b8418f
Merge pull request #255 from ellbee/stores-doc
acdlite 50c496c
Merge branch 'improve-docs' of https://github.com/gaearon/redux into …
acdlite 9dd724d
Getting Started guide
acdlite bfc8c26
Fix page title
acdlite 126ac3e
Typo
acdlite 3ade4b0
combine => combineReducers
acdlite 6805327
Update store.md
ellbee f5a90cd
Update store.md
ellbee 9050a5f
Reference react-redux within react.md
mcamac cdaa3e8
Merge pull request #260 from mcamac/improve-docs-react
gaearon 8d78e49
Update store.md
ellbee 1d207ba
Update store.md
ellbee 2bb3769
Update store.md
ellbee 2afae08
Merge pull request #258 from ellbee/stores-doc
gaearon a6f16ee
Remove extra 'a' in doc
chentsulin 224aef7
Merge pull request #269 from chentsulin/patch-1
gaearon 1452d8b
Fix a broken link in middleware.md
chentsulin 82532a4
Merge pull request #280 from chentsulin/patch-2
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
### Philosophy & Design Goals | ||
|
||
* You shouldn't need a book on functional programming to use Redux. | ||
* Everything (Stores, Action Creators, configuration) is hot reloadable. | ||
* Preserves the benefits of Flux, but adds other nice properties thanks to its functional nature. | ||
* Prevents some of the anti-patterns common in Flux code. | ||
* Works great in [universal (aka “isomorphic”)](https://medium.com/@mjackson/universal-javascript-4761051b7ae9) apps because it doesn't use singletons and the data can be rehydrated. | ||
* Doesn't care how you store your data: you may use JS objects, arrays, ImmutableJS, etc. | ||
* Under the hood, it keeps all your data in a tree, but you don't need to think about it. | ||
* Lets you efficiently subscribe to finer-grained updates than individual Stores. | ||
* Provides hooks for powerful devtools (e.g. time travel, record/replay) to be implementable without user buy-in. | ||
* Provides extension points so it's easy to [support promises](https://github.com/gaearon/redux/issues/99#issuecomment-112212639) or [generate constants](https://gist.github.com/skevy/8a4ffc3cfdaf5fd68739) outside the core. | ||
* No wrapper calls in your stores and actions. Your stuff is your stuff. | ||
* It's super easy to test things in isolation without mocks. | ||
* You can use “flat” Stores, or [compose and reuse Stores](https://gist.github.com/gaearon/d77ca812015c0356654f) just like you compose Components. | ||
* The API surface area is minimal. | ||
* Have I mentioned hot reloading yet? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## Examples | ||
|
||
### Simple Examples | ||
|
||
Redux is distributed with a Counter and a TodoMVC example in its source code. | ||
|
||
First, clone the repo: | ||
|
||
``` | ||
git clone https://github.com/gaearon/redux.git | ||
cd redux | ||
``` | ||
|
||
Run the Counter example: | ||
|
||
``` | ||
cd redux/examples/counter | ||
npm install | ||
npm start | ||
``` | ||
|
||
Run the TodoMVC example: | ||
|
||
``` | ||
cd ../todomvc | ||
npm install | ||
npm start | ||
``` | ||
|
||
### Async and Universal Examples with Routing | ||
|
||
These async and [universal (aka “isomorphic”)](https://medium.com/@mjackson/universal-javascript-4761051b7ae9) examples using React Router should help you get started: | ||
|
||
* [redux-react-router-async-example](https://github.com/emmenko/redux-react-router-async-example): Work in progress. Semi-official. Only the client side. Uses React Router. | ||
* [react-redux-universal-hot-example](https://github.com/erikras/react-redux-universal-hot-example): Universal. Uses React Router. | ||
* [redux-example](https://github.com/quangbuule/redux-example): Universal. Uses Immutable, React Router. | ||
* [isomorphic-counter-example](https://github.com/khtdr/redux-react-koa-isomorphic-counter-example): Universal. A bare-bone implentation of the [counter example app](https://github.com/gaearon/redux/tree/master/examples/counter). Uses promises-middleware to interact with API via Koa on the server. | ||
|
||
Don’t be shy, add your own! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
## FAQ | ||
|
||
### How does hot reloading work? | ||
|
||
* http://webpack.github.io/docs/hot-module-replacement.html | ||
* http://gaearon.github.io/react-hot-loader/ | ||
* Literally that's it. Redux is fully driven by component props, so it works on top of React Hot Loader. | ||
|
||
### Can I use this in production? | ||
|
||
Yep. People already do that although I warned them! The API surface is minimal so migrating to 1.0 API when it comes out won't be difficult. Let us know about any issues. | ||
|
||
### How do I do async? | ||
|
||
There's already a built-in way of doing async action creators: | ||
|
||
```js | ||
// Can also be async if you return a function | ||
export function incrementAsync() { | ||
return dispatch => { | ||
setTimeout(() => { | ||
// Yay! Can invoke sync or async actions with `dispatch` | ||
dispatch(increment()); | ||
}, 1000); | ||
}; | ||
} | ||
``` | ||
|
||
It's also easy to implement support for returning Promises or Observables with a custom middleware. [See an example of a custom Promise middleware.](https://github.com/gaearon/redux/issues/99#issuecomment-112212639) | ||
|
||
### But there are switch statements! | ||
|
||
`(state, action) => state` is as simple as a Store can get. You are free to implement your own `createStore`: | ||
|
||
```js | ||
export default function createStore(initialState, handlers) { | ||
return (state = initialState, action) => | ||
handlers[action.type] ? | ||
handlers[action.type](state, action) : | ||
state; | ||
} | ||
``` | ||
|
||
and use it for your Stores: | ||
|
||
```js | ||
export default createStore(0, { | ||
[INCREMENT_COUNTER]: x => x + 1, | ||
[DECREMENT_COUNTER]: x => x - 1 | ||
}); | ||
``` | ||
|
||
It's all just functions. | ||
Fancy stuff like generating stores from handler maps, or generating action creator constants, should be in userland. | ||
Redux has no opinion on how you do this in your project. | ||
|
||
See also [this gist](https://gist.github.com/skevy/8a4ffc3cfdaf5fd68739) for an example implementation of action constant generation. | ||
|
||
### What about `waitFor`? | ||
|
||
I wrote a lot of vanilla Flux code and my only use case for it was to avoid emitting a change before a related Store consumes the action. This doesn't matter in Redux because the change is only emitted after *all* Stores have consumed the action. | ||
|
||
If several of your Stores want to read data from each other and depend on each other, it's a sign that they should've been a single Store instead. [See this discussion on how `waitFor` can be replaced by the composition of stateless Stores.](https://gist.github.com/gaearon/d77ca812015c0356654f) | ||
|
||
### My views aren't updating! | ||
|
||
Redux makes a hard assumption that you never mutate the state passed to you. It's easy! For example, instead of | ||
|
||
```js | ||
function (state, action) { | ||
state.isAuthenticated = true; | ||
state.email = action.email; | ||
return state; | ||
} | ||
``` | ||
|
||
you should write | ||
|
||
```js | ||
function (state, action) { | ||
return { | ||
...state, | ||
isAuthenticated: true, | ||
email: action.email | ||
}; | ||
} | ||
``` | ||
|
||
[Read more](https://github.com/sebmarkbage/ecmascript-rest-spread) about the spread properties ES7 proposal. | ||
|
||
### How do Stores, Actions and Components interact? | ||
|
||
Action creators are just pure functions so they don't interact with anything. Components need to call `dispatch(action)` (or use `bindActionCreators` that wraps it) to dispatch an action *returned* by the action creator. | ||
|
||
Stores are just pure functions too so they don't need to be “registered” in the traditional sense, and you can't subscribe to them directly. They're just descriptions of how data transforms. So in that sense they don't “interact” with anything either, they just exist, and are used by the dispatcher for computation of the next state. | ||
|
||
Now, the dispatcher is more interesting. You pass all the Stores to it, and it composes them into a single Store function that it uses for computation. The dispatcher is also a pure function, and it is passed as configuration to `createRedux`, the only stateful thing in Redux. By default, the default dispatcher is used, so if you call `createRedux(stores)`, it is created implicitly. | ||
|
||
To sum it up: there is a Redux instance at the root of your app. It binds everything together. It accepts a dispatcher (which itself accepts Stores), it holds the state, and it knows how to turn actions into state updates. Everything else (components, for example) subscribes to the Redux instance. If something wants to dispatch an action, they need to do it on the Redux instance. `Connector` is a handy shortcut for subscribing to a slice of the Redux instance's state and injecting `dispatch` into your components, but you don't have to use it. | ||
|
||
There is no other “interaction” in Redux. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implentation -> implementation