Giter Club home page Giter Club logo

react-stasis's Introduction

react-stasis

Universal (isomorphic) rendering is one of the benefits of using React. Often times, however, a large portion of the page is static, so ideally you would want to to render it on the server, but not worry about it on the client side.

Unfortunately when React checks if the markup can be reused on the client side, it's currently impossible to tell it to ignore a certain subtree. That means that if you are unable to replicate DOM exactly, reconciliation will fail.

This is where react-stasis comes in. This package allows to you to only render part of the React tree on the server thus possibly reducing data sent to the client and the app initialization time.

In case you want some dynamic elements somewhere deep inside your static tree, react-stasis has you covered with a portal functionality

Basics

react-stasis provides a wrapper around react-dom APIs, so usually on the server side you just need to install react-stasis NPM package and then change react-dom/server import to react-stasis/server.

On the client side you also need change the import of react-dom to react-stasis.

In the React application itself, the only thing you need to do is to wrap a static subtree in a Static component provided by react-stasis and only include it's children on the server-side:

const {Component} = require('react');
const {Static, render} = require('react-stasis');

class MyApp extends Component {
    render() {
        return <div>
            <div>Some dynamic part of your app</div>
            <Static>
                {this.renderStatic()}
            </Static>
        </div>
    }
    renderStatic() {
        // this can be used for example by webpack
        // to exclude the statis part from the bundle
        // if you use https://webpack.github.io/docs/list-of-plugins.html#defineplugin
        if (process.env.TARGET === 'browser') {
            return null;
        } else {
            const MyStaticComponent = require('./my-static-component');
            return <MyStaticComponent someFoo="bar"/>;
        }
    }
}

Portals

Portals allow you to have some dynamic React components inside your sub tree. A good example would be a slider with images in a long page of text. Here's how it will roughly look like:

const {Component} = require('react');
const {Static, Portal, render} = require('react-stasis');

class MyApp extends Component {
    render() {
        return <div>
            <div>Some dynamic part of your app</div>
            <Static>
                {this.renderStatic(
                    <MySlider />
                )}
            </Static>
        </div>
    }
    renderStatic(slider) {
        // this can be used for example by webpack
        // to exclude the statis part from the bundle
        // if you use https://webpack.github.io/docs/list-of-plugins.html#defineplugin
        const portal =
            <Portal name="slider">
                <MySlider />
            </Portal>
        if (process.env.TARGET === 'browser') {
            return portal;
        } else {
            const MyStaticComponent = require('./my-static-component');
            return <MyStaticComponent someFoo="bar">
                {portal}
            </MyStaticComponent>;
        }
    }
}

The example above only assumes that MyStaticComponent can accept Slider as a child and knows where to place it in the content.

Fully Working Example

To see a fully-working example, but which is really bare bones (without a server-side framework or a bundler) you can take a look at the example folder.

You can also clone this repo and then run:

npm install
npm run example

and see how it works out in the browser.

You shouldn't use this example as a starting point for your React app โ€” look at react-create-app instead.

Known issues

  • Using react-stasis just in the browser will result in an error.

  • Nested Static components are not supported at the moment and will result in an error in the browser.

  • Due to a difference in reported markup between browser-generated innerHTML and what ReactDOM.renderToStaticMarkup generates, react-stasis has to go a global replace /> to >, so if you have some html markup as text (static data) in your app, it might be affected. If you don't need to render, it's recommended to escape this markup beforehand.

Static component props

  • component: React.Component | string
  • props: object

react-stasis's People

Contributors

grassator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.