Update
This commit is contained in:
parent
bc7e126cd5
commit
f4c111a50b
|
@ -55,7 +55,10 @@ fetchData = (url) => (dispatch) => {
|
||||||
.catch((error) => dispatch({ type: 'FETCH_ERROR', error })
|
.catch((error) => dispatch({ type: 'FETCH_ERROR', error })
|
||||||
})
|
})
|
||||||
|
|
||||||
store.dispatch(fetchData('http://google.com'))
|
store.dispatch(fetchData('/posts'))
|
||||||
|
|
||||||
|
// That's actually shorthand for:
|
||||||
|
fetchData('/posts')(store.dispatch)
|
||||||
```
|
```
|
||||||
|
|
||||||
### [redux-effects](https://www.npmjs.com/package/redux-effects)
|
### [redux-effects](https://www.npmjs.com/package/redux-effects)
|
||||||
|
@ -75,25 +78,22 @@ Pass side effects declaratively to keep your actions pure.
|
||||||
```
|
```
|
||||||
|
|
||||||
### [redux-thunk](https://www.npmjs.com/package/redux-thunk)
|
### [redux-thunk](https://www.npmjs.com/package/redux-thunk)
|
||||||
Pass "thunks" to as actions.
|
Pass "thunks" to as actions. Extremely similar to redux-promises, but has support for getState.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function fetchPosts () {
|
fetchData = (url) => (dispatch, getState) => {
|
||||||
// Usually returns a promise.
|
dispatch({ type: 'FETCH_REQUEST' })
|
||||||
return function (dispatch, getState) {
|
fetch(url)
|
||||||
dispatch({ type: 'posts:fetch' })
|
.then((data) => dispatch({ type: 'FETCH_DONE', data })
|
||||||
return API.get('/posts')
|
.catch((error) => dispatch({ type: 'FETCH_ERROR', error })
|
||||||
.then(payload => dispatch({ type: 'posts:fetch:done', payload })
|
})
|
||||||
.then(error => dispatch({ type: 'posts:fetch:error', error })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
store.dispatch(fetchPosts())
|
store.dispatch(fetchData('/posts'))
|
||||||
|
|
||||||
// That's actually shorthand for:
|
// That's actually shorthand for:
|
||||||
fetchPosts()(store.dispatch, store.getState)
|
fetchData('/posts')(store.dispatch, store.getState)
|
||||||
|
|
||||||
// Optional: since fetchPosts returns a promise, it can be chained
|
// Optional: since fetchData returns a promise, it can be chained
|
||||||
// for server-side rendering
|
// for server-side rendering
|
||||||
store.dispatch(fetchPosts()).then(() => {
|
store.dispatch(fetchPosts()).then(() => {
|
||||||
ReactDOMServer.renderToString(<MyApp store={store} />)
|
ReactDOMServer.renderToString(<MyApp store={store} />)
|
||||||
|
|
Loading…
Reference in New Issue