0.1.3 • Published 7 years ago

redux-ready v0.1.3

Weekly downloads
17
License
MIT
Repository
github
Last release
7 years ago

redux-ready

Enhances your redux store with a store.ready() method, which resolves once all promises dispatched into the store are fulfilled.

Very handy for server rendering redux applications that initialise with async actions.

Install

npm install redux-ready --save

### Set up

var withReady = require("redux-ready");

var storeEnhancer = compose(
  withReady, // make sure this comes first
  applyMiddleware(thunk)
);

var store = createStore(reducers, storeEnhancer);

Usage

var app = <Provider store={store}><App /></Provider>;

// Render the app initially to dispatch any actions in components' lifecycle
var html = renderToString(app);

// Wait for async actions to resolve
store
  .ready()
  .then(state => {
    // Re-render app with the async data now in the store
    html = renderToString(app);

    // Serve the app html and state
    res.status(200).render("reactView", { html, state });
  })
  .catch(error => {
    // Serve an error page, or aleternatively, the app with an unresolved state
    res.state(500).render("errorView", { error });
  });