Coder Social home page Coder Social logo

isomorphic-react-renderer's Introduction

Isomorphic React

Why

You may want to do use this if you are rendering a React template server side with some fallback content / content for SEO then on the client want to update this React template with some new data that is post loaded.

If you don't pass the React properties to the client, your entire React component will re-render which isn't very performant. By using this the React template will do a partial re-render.

This problem is common and you can read about it on the internet. This is my solution. If it's not suitable here are some alternatives and articles which discuss this problem at length.

Install

npm install isomorphic-react

Usage

Example component

var React = require('react');

module.exports React.createClass({
  getInitialState: function () {
    return {
      name: this.props.name
    };
  },
  render: function render() {
    return React.DOM.div({
      className:'hello-name'
    }, this.state.name);
  }
});

On the server

var express = require('express');
var app = express();
var isomorphicReactLib = require('isomorphic-react');
var ExampleComponent = require('example-component');

var htmlDOM = isomorphicReactLib.createComponent(
  'component-name', 
  ExampleComponent, 
  {name: 'Adam Bulmer'}
);

app.get('/', function (req, res) {
  res.send(htmlDOM);
});

On the client

var React = require('react');
var ReactDOM = require('react-dom');

var isomorphicReactLib = require('isomorphic-react');
var ExampleComponent = require('example-component');

var props = isomorphicReactLib.getComponentProps('component-name');
var exampleComponent = React.createFactory(ExampleComponent);

var componentReference = ReactDOM.render(exampleComponent(props), document.querySelector('.same-mount-point-as-server'));

var newName = 'John Smith';

componentReference.setState({name: newName});

isomorphic-react-renderer's People

Contributors

mintuz avatar

Stargazers

 avatar

Forkers

tej-s

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.