ReduxConnect for React Router
How do you usually request data and store it to redux state? You create actions that do async jobs to load data, create reducer to save this data to redux state, then connect data to your component or container.
Usually it's very similar routine tasks.
Also, usually we want data to be preloaded. Especially if you're building universal app, or you just want pages to be solid, don't jump when data was loaded.
This package consist of 2 parts: one part allows you to delay containers rendering until some async actions are happening. Another stores your data to redux state and connect your loaded data to your container.
Notice
This is a fork and refactor of redux-async-connect
Installation & Usage
Using npm:
$ npm install redux-connect -S
// 1. Connect your data, similar to react-redux @connect@Component { // 2. access data as props const lunch route = thisprops return <div> lunchname </div> } component { return <div>'child component'</div> } const routes = path: '/' component: App routes: path: '/child' exact: true component: Child // 2. Connect redux async reducerconst store = // 3. Render `Router` with ReduxAsyncConnect middleware
Server
app { return ` <!doctype html> <html> <body> <div id="app"></div> <!-- its a Redux initial data --> <script type="text/javascript"> window.__data=; </script> </body> </html> `}
API
ImmutableJS
Usage with This lib can be used with ImmutableJS or any other immutability lib by providing methods that convert the state between mutable and immutable data. Along with those methods, there is also a special immutable reducer that needs to be used instead of the normal reducer.
; // Set the mutability/immutability functions;; // Thats all, now just use redux-connect as normalconst rootReducer =
Comparing with other libraries
There are some solutions of problem described above:
- AsyncProps It solves the same problem, but it doesn't work with redux state. Also it's significantly more complex inside, because it contains lots of logic to connect data to props. It uses callbacks against promises...
- react-fetcher It's very simple library too. But it provides you only interface for decorating your components and methods to fetch data for them. It doesn't integrated with React Router or Redux. So, you need to write you custom logic to delay routing transition for example.
- react-resolver Works similar, but isn't integrated with redux.
Redux Connect uses awesome Redux to keep all fetched data in state. This integration gives you agility:
- you can react on fetching actions like data loading or load success in your own reducers
- you can create own middleware to handle Redux Async Connect actions
- you can connect to loaded data anywhere else, just using simple redux @connect
- finally, you can debug and see your data using Redux Dev Tools
Also it's integrated with React Router to prevent routing transition until data is loaded.
Contributors
Collaboration
You're welcome to PR, and we appreciate any questions or issues, please open an issue!