Git Product home page Git Product logo

isomorphicreactwithredux5's Introduction

IsomorphicReactWithRedux5

To manage State we want to integrate with Redux. However there are 4 problems

  • Redux needs different configuration on browser vs server
  • Aspects of authentication needs to be handled in the server Normally this is only on browser
  • Needs someway to detect when all initial data load action creators are completed on server.
  • Need state rehydration on browser

Solving few in this

created seperate stores for server & client

  • server side:

1 getting store seperately

app.get('*', (req,res) => {
    const store = createStore();
    console.log('---server store', store);
    // some logic to initialize & load the data into the store
    res.send(renderer(req, store));
});

2 createStore is

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducers from '../client/reducers';

export default () => {
   const store = createStore(reducers, {}, applyMiddleware(thunk));
   return store;
}

4 now renderer gets that store & renders as per store if store is empty it renders empty but then client hydrates on top of it

export default (req, store) => {
    console.log('--------taken', store);
    const content = renderToString(
        <Provider store={store}>
            <StaticRouter location={req.path} context={{}}>
                <Routes/>
            </StaticRouter>
        </Provider>
    );
    console.log('----ser hand', content);    
    return `
        <html>
            <head>
                <title>react ssr app</title>
            </head>
            <body>
                <div id="root">${content}</div>
                <script src="bundle.js"></script>
            </body>
        </html>
    `;
}
  • client side:

main client is regular SPA with react & redux

//Start up point for the client side application
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import {Provider} from 'react-redux';
import Routes from './Routes';
import reducers from './reducers';

const store = createStore(reducers, {}, applyMiddleware(thunk));

ReactDOM.hydrate(
    <Provider store={store}>
        <BrowserRouter> 
            <Routes />
        </BrowserRouter>
    </Provider>
, document.querySelector('#root')
);

isomorphicreactwithredux5's People

Contributors

venkatachadalawada avatar

Watchers

James Cloos avatar  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.