This commit is contained in:
Rico Sta. Cruz 2016-06-01 12:28:31 +08:00
parent bc7e126cd5
commit f4c111a50b
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 14 additions and 14 deletions

View File

@ -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} />)