项目作者: peter7z

项目描述 :
A redux middleware that lets you use your own data fetching and expose the results in a suspense compatible way
高级语言: JavaScript
项目地址: git://github.com/peter7z/suspense-redux.git
创建时间: 2019-11-11T22:58:52Z
项目社区:https://github.com/peter7z/suspense-redux

开源协议:ISC License

下载


Version
License

redux-suspense

A redux middleware that lets you use your own data fetching and expose the results in a suspense compatible way

Caution:

  • Suspense API is an experimental features that is not yet available in a stable React release.

Basic usage guide

npm install redux-suspense

Step 1 of 4: Suspense reducer

Add the reducer

  1. // src/reducers/index.js
  2. import { combineReducers } from 'redux'
  3. import { suspenseReducer } from 'redux-suspense'
  4. const rootReducer = combineReducers({
  5. // ...your other reducers here
  6. // you have to pass suspenseReducer under 'suspense' key,
  7. suspense: suspenseReducer
  8. })

Step 2 of 4: Suspense middleware

  1. import { createStore, applyMiddleware } from 'redux';
  2. import { suspenseMiddleware } from 'redux-suspense';
  3. import rootReducer from 'src/reducers/index';
  4. const store = createStore(rootReducer, applyMiddleware(suspenseMiddleware));

Step 3 of 4: Create a resource

  1. // src/resources/postsResources.js
  2. import { createResource } from 'redux-suspense';
  3. export const postsResource = createResource(
  4. 'POSTS',
  5. fetchPosts, // this is your async function that fetches and returns the posts
  6. )

If you want to store the posts in your own reducer just pass a selector to createResource. For example:

  1. export const postsResource = createResource(
  2. 'POSTS',
  3. fetchPosts,
  4. state => state.yourPostsReducer.posts
  5. )
  6. // Then you can export the success action to handle it in your reducer.
  7. export const { success: fetchPostsSuccess } = postsResource

createResource will create a couple more things:

  • resourceName This is the same string you passed as first argument.
  • handler The function you passed as the second argument.
  • success, error and request These are action creators. All of which have a toString() method. In the example above succes.toString() would evaluate to 'POSTS_SUCCESS'
  • selector The selector you passed as the third argument.

Step 4 of 4: Consume a resource

Dispatch the resource as an action, as soon as you want to start fetching data. And use useResource as if it was a selector, inside a <Suspense> tag.

  1. import React, { Suspense, useEffect } from 'react'
  2. import { useDispatch } from 'react-redux'
  3. import { postsResource } from 'src/resources/postsResources.js'
  4. function App () {
  5. const dispatch = useDispatch()
  6. useEffect(() => dispatch(postsResource), [])
  7. return (
  8. <Suspense fallback={<h1> Loading...</h1>}>
  9. <Posts ></Posts>
  10. </Suspense>
  11. )
  12. }
  1. import { useResource } from 'redux-suspense'
  2. import { postsResource } from 'src/resources/postsResources.js'
  3. export function Posts() {
  4. const posts = useResource(postsResource)
  5. return (
  6. <div>
  7. {posts}
  8. </div>
  9. )
  10. }

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the ISC License - see the LICENSE file for details