Coder Social home page Coder Social logo

redux-async-render's Introduction

Redux Async Render

A server-side rendering helper for redux.

So what's this?

Simply a StoreEnhancer for redux that catches all promises from "dispatch" calls and stores them in "store.promises". It also provides a function called renderAsync that awaits all promises before returning a final rendered version.

This is not an alternative to redux-promise and it does nothing on the client-side.

Example Usage

store.js

import {createStore} from 'redux';
import reducers from 'reducers';

export default function configureStore(initialState) {
  const store = createStore(reducers, initialState);
  return store;
}

server-store.js

import {serverHelper} from 'redux-async-render';
import {createStore, compose} from 'redux';
import reducers from 'reducers';

const finalCreateStore = compose(
  serverHelper
)(createStore);

export default function configureStore(initialState) {
  const store = finalCreateStore(reducers, initialState);
  return store;
}

server-render.jsx

import {renderToString} from 'react-dom';
import {renderAsync} from 'redux-async-render';
import configureStore from './server-store';

const html = `
<html>
  <head>
    <title><!-- TITLE --></title>
  </head>
  <body>
    <div id="root"><!-- CONTENT --></div>
  </body>
  <script>
    window.initialStoreData = "-- STORES --";
  </script>
  <script src="/bundle.js"></script>
</html>
`;

export function express(req, res, next) {
  const store = configureStore();
  const renderFn = () => renderToString(<App store={store} />);

  renderAsync(store, renderFn).then(rendered => {
    const state = store.getState();
    res.status(200).send(
      html
        .replace('<!-- CONTENT -->', rendered)
        .replace('"-- STORES --"', JSON.stringify(state))
    );
  }).catch(next);
}

Notes

  • It's suggested that you use a custom ajax fetch function that calls the express server (or whatever you use) internally.

Known issues

  • Calling ReactDOMServer.renderToString will only cause componentWillMount to be called. componentDidMount won't be called. Moreover, componentWillMount will be called multiple times in each server-render so make sure you don't fetch data twice or you might be stuck in an infinite loop. This is a limitation of ReactDOMServer but hopefully it will change in the future.

redux-async-render's People

Contributors

louy avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.