redux-fetch-data
Redux utility library for fetching data using promises on both server and client.
Install
npm install redux-fetch-data --save
Usage
Initial setup
On the server
Here is an example setup of a simple server. In this example we used Express, but any server framework will do.
;;;;;;;; ; const app = ; // Renders the actual HTML page { return ` <!doctype html> <html> <body> <div id="root"></div> <script dangerouslySetInnerHTML={{__html: `window__INITIAL_STATE__=$JSON;`}} charSet="UTF-8"/> </body> </html> `;} // Register the rendering middlewareapp; app;
On the client
Here is an example of a client-side entry script.
;;;;;; ; // Hydrate the initial state from the server stateconst initialState = window__INITIAL_STATE__;const store = ; ;
Fetching data
Instead of loading data in componentWillMount
, move that logic to a static fetchData
method.
This method should return a promise. Also, make sure you only fetch data from your containers
(top-level components), and pass down the data as props to sub-components.
static { // this method should return a promise } ...
Protip! You can use Promise.all
to combine multiple promises into one.
Tests
Run the test suite:
npm test
Run the test suite in watch mode:
npm run test:watch <path>
Generate the code coverage report:
npm run test:cover
License
See LICENSE.