redux-async-loader
Async data loader for Redux apps with React-Router.
Installation
npm install --save redux-async-loader
Usage
1. Register Reducer
;; const store = ;
2. Server-Side Rendering (Optional)
;; ;
3. Client-Side Rendering
;;; const RenderWithMiddleware = ; ;
If you are using react-router-scroll, it should be applied after redux-async-loader.
; const RenderWithMiddleware = ;
4. Async data loading by routing (both client and server-side rendering)
;; Component // ... store UserList ;
or, with decorator:
@@Component // ...
or, with recompose:
; exptends ReactComponent // ...;
Unlike redux-async-connect, redux-async-loader itself doesn't connect to store. You have to call connect() explicitly if you want to use store's state.
If you want to invoke asyncLoader()
when just querystring (not path) is changed, you must specify key names of querystring to router.
<Route path="items" component=ItemList asyncLoaderProps={{queryKeys: "q, page"}} ... />
Or, you can use the wildcard for any keys of querystring:
<Route path="items" component=ItemList asyncLoaderProps={{queryKeys: "*"}} ... />
5. Async data loading by mounting/updating (client-side rendering only)
;; Component // ... store UserList ;
or, with decorator:
@@Component // ...
or, with recompose:
; exptends ReactComponent // ...;
API
asyncLoader(loader)
Creates Higher-order Component for async data loading by routing.
Arguments
loader
(Function): Called when this component is routed.- Arguments
props
(Object): The props passed from React-Router. See React-Router API docs for more info.store
: (Object): Redux's store object.
- Returns
- (Promise): Fulfilled when data loading is completed.
- Arguments
Returns
- (Function): Creates higher-order component.
- Arguments
- wrappedComponent (Component): Wrapped component.
- Returns
- (Component): Wrapper component.
- Arguments
deferLoader(loader)
Creates Higher-order Component for async data loading by mounting and updating.
Arguments
loader
(Function): Called when this component is mounted or updated.- Arguments
props
(Object): The props passed from parent component.store
: (Object): Redux's store object.
- Returns
- (Promise): Fulfilled when data loading is completed.
- Arguments
Returns
- (Function): Creates higher-order component.
- Arguments
- wrappedComponent (Component): Wrapped component.
- Returns
- (Component): Wrapper component.
- Arguments
loadOnServer(renderProps, store)
Loads async data on the server side.
Arguments
renderProps
(Object): The props passed viamatch()
's callback. See React-Router API docs for more info.store
(Object): Redux's store object.
Returns
- (Promise): Fulfilled when data loading is completed.