2016-01-07 04:47:48 +00:00
|
|
|
---
|
2017-08-30 13:52:08 +00:00
|
|
|
title: Awesome Redux
|
|
|
|
category: React
|
2020-07-04 13:33:09 +00:00
|
|
|
updated: 2017-11-19
|
2016-01-07 04:47:48 +00:00
|
|
|
---
|
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
### redux-actions
|
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
Create action creators in flux standard action format.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-07 04:47:48 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
increment = createAction('INCREMENT', amount => amount)
|
|
|
|
increment = createAction('INCREMENT') // same
|
2017-08-30 13:52:08 +00:00
|
|
|
```
|
2016-01-07 04:47:48 +00:00
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
```js
|
2016-01-07 04:47:48 +00:00
|
|
|
increment(42) === { type: 'INCREMENT', payload: 42 }
|
2017-08-30 13:52:08 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Errors are handled for you:
|
|
|
|
err = new Error()
|
2016-01-07 04:47:48 +00:00
|
|
|
increment(err) === { type: 'INCREMENT', payload: err, error: true }
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-actions](https://www.npmjs.com/package/redux-actions)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
2017-11-19 17:19:44 +00:00
|
|
|
### flux-standard-action
|
2017-08-30 13:52:08 +00:00
|
|
|
|
2016-01-17 16:05:50 +00:00
|
|
|
A standard for flux action objects. An action may have an `error`, `payload` and `meta` and nothing else.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-07 04:47:48 +00:00
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
```js
|
2016-01-07 04:47:48 +00:00
|
|
|
{ type: 'ADD_TODO', payload: { text: 'Work it' } }
|
|
|
|
{ type: 'ADD_TODO', payload: new Error(), error: true }
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[flux-standard-action](https://github.com/acdlite/flux-standard-action)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### redux-multi
|
|
|
|
|
2016-06-01 04:25:44 +00:00
|
|
|
Dispatch multiple actions in one action creator.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-06-01 04:25:44 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
store.dispatch([
|
|
|
|
{ type: 'INCREMENT', payload: 2 },
|
|
|
|
{ type: 'INCREMENT', payload: 3 }
|
|
|
|
])
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-multi](https://github.com/ashaffer/redux-multi)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### reduce-reducers
|
2016-06-01 05:12:45 +00:00
|
|
|
Combines reducers (like *combineReducers()*), but without namespacing magic.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-06-01 05:12:45 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
re = reduceReducers(
|
|
|
|
(state, action) => state + action.number,
|
|
|
|
(state, action) => state + action.number
|
|
|
|
)
|
|
|
|
|
|
|
|
re(10, { number: 2 }) //=> 14
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[reduce-reducers](https://www.npmjs.com/package/reduce-reducers)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### redux-logger
|
|
|
|
|
2016-06-02 23:01:18 +00:00
|
|
|
Logs actions to your console.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Nothing to see here
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-logger](https://github.com/evgenyrodionov/redux-logger)
|
|
|
|
{: .-crosslink}
|
2016-06-01 05:24:33 +00:00
|
|
|
|
2016-06-01 04:25:44 +00:00
|
|
|
Async
|
|
|
|
-----
|
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
### redux-promise
|
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
Pass promises to actions. Dispatches a flux-standard-action.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-07 04:47:48 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
increment = createAction('INCREMENT') // redux-actions
|
|
|
|
increment(Promise.resolve(42))
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-promise](https://github.com/acdlite/redux-promise)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### redux-promises
|
|
|
|
|
2016-01-17 16:05:50 +00:00
|
|
|
Sorta like that, too. Works by letting you pass *thunks* (functions) to `dispatch()`. Also has 'idle checking'.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-17 16:05:50 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
fetchData = (url) => (dispatch) => {
|
|
|
|
dispatch({ type: 'FETCH_REQUEST' })
|
|
|
|
fetch(url)
|
|
|
|
.then((data) => dispatch({ type: 'FETCH_DONE', data })
|
|
|
|
.catch((error) => dispatch({ type: 'FETCH_ERROR', error })
|
|
|
|
})
|
|
|
|
|
2016-06-01 04:28:31 +00:00
|
|
|
store.dispatch(fetchData('/posts'))
|
2017-08-30 13:52:08 +00:00
|
|
|
```
|
2016-06-01 04:28:31 +00:00
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
```js
|
2016-06-01 04:28:31 +00:00
|
|
|
// That's actually shorthand for:
|
|
|
|
fetchData('/posts')(store.dispatch)
|
2016-01-17 16:05:50 +00:00
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-promises](https://www.npmjs.com/package/redux-promises)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### redux-effects
|
|
|
|
|
2016-01-07 04:47:48 +00:00
|
|
|
Pass side effects declaratively to keep your actions pure.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-07 04:47:48 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
type: 'EFFECT_COMPOSE',
|
|
|
|
payload: {
|
|
|
|
type: 'FETCH'
|
|
|
|
payload: {url: '/some/thing', method: 'GET'}
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
steps: [ [success, failure] ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-effects](https://www.npmjs.com/package/redux-effects)
|
|
|
|
{: .-crosslink}
|
2017-08-30 13:52:08 +00:00
|
|
|
|
|
|
|
### redux-thunk
|
|
|
|
|
2016-06-01 04:28:31 +00:00
|
|
|
Pass "thunks" to as actions. Extremely similar to redux-promises, but has support for getState.
|
2017-08-30 13:52:08 +00:00
|
|
|
{: .-setup}
|
2016-01-07 04:47:48 +00:00
|
|
|
|
|
|
|
```js
|
2016-06-01 04:28:31 +00:00
|
|
|
fetchData = (url) => (dispatch, getState) => {
|
|
|
|
dispatch({ type: 'FETCH_REQUEST' })
|
|
|
|
fetch(url)
|
|
|
|
.then((data) => dispatch({ type: 'FETCH_DONE', data })
|
|
|
|
.catch((error) => dispatch({ type: 'FETCH_ERROR', error })
|
|
|
|
})
|
2016-06-01 04:25:44 +00:00
|
|
|
|
2016-06-01 04:28:31 +00:00
|
|
|
store.dispatch(fetchData('/posts'))
|
2017-08-30 13:52:08 +00:00
|
|
|
```
|
2016-06-01 04:25:44 +00:00
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
```js
|
2016-06-01 04:25:44 +00:00
|
|
|
// That's actually shorthand for:
|
2016-06-01 04:28:31 +00:00
|
|
|
fetchData('/posts')(store.dispatch, store.getState)
|
2017-08-30 13:52:08 +00:00
|
|
|
```
|
2016-06-01 04:25:44 +00:00
|
|
|
|
2017-08-30 13:52:08 +00:00
|
|
|
```js
|
2016-06-01 04:28:31 +00:00
|
|
|
// Optional: since fetchData returns a promise, it can be chained
|
2016-06-01 04:25:44 +00:00
|
|
|
// for server-side rendering
|
|
|
|
store.dispatch(fetchPosts()).then(() => {
|
|
|
|
ReactDOMServer.renderToString(<MyApp store={store} />)
|
|
|
|
})
|
2016-01-07 04:47:48 +00:00
|
|
|
```
|
2017-08-30 13:52:08 +00:00
|
|
|
|
2017-09-04 06:19:43 +00:00
|
|
|
[redux-thunk](https://www.npmjs.com/package/redux-thunk)
|
|
|
|
{: .-crosslink}
|