Giter Site home page Giter Site logo

react-view-engine's Introduction

This is a express view engine for allowing server rendering of react components.

#Installation

    npm install react-view-engine --save

If you do not have a .babelrc file, create and set react in a .babelrc file

echo '{"presets": ["react", "es2015", "stage-0"]}' > .babelrc

#Usage

    /**
     * app.js
     */
    var ReactEngine = require('react-view-engine')
    app.engine('jsx', ReactEngine(/*options?*/))    //set jsx files to use ReactEngine
    app.engine('jade', require('jade').__express)   //Still can use other engines if needed.
    app.set('view engine', 'jsx')                   //Set defaut view extension to be jsx
    app.set('views', [
      __dirname + '/components',                    //jsx view folder
      __dirname + '/public/views'                   //jade view folder
    ])

After this you can use res.render function from express to render jsx.

    app.get('/', function index(req, res) {
      res.render('client-app', { name: 'Joel' })    //render(<react file>, <initial props>)
    }

Will render the View...

    /**
     * components/client-app.jsx
    */

    var React = require('react');

    var ClientApp = React.createClass({
      render: function() {
        return (
          <html>
            <head>
              <script src="/js/client.js"/>
            </head>
            <body>
              <h1>{this.props.name}</h1>
            </body>
          </html>
        );
      }
    });

    module.exports = ClientApp;

In order to get inital props on client side loadProps(View, props, layout) is called (after DOMContentLoaded has been fired). Set this function on the global state if you wish mount your react component clientside.

    /**
     * components/app.jsx
     * Entry point for bundler
    */
    var ClientApp = require('./client-app'); //require the views you will render serverside for the bundler.
    var ReactDOM = require('react-dom');
    var React = require('react');

    function loadProps(name, props, layout){
        var rootComponent = require('./'+name);
        ReactDOM.render(<rootComponent {...props} />, document)
    }

#Options

layout: A component to wrap all your components with. This in nice if you do not want to write <html> tags in all of your views. The same props object will be given to both your layout and your View component.

extension: file extensions for babel-register to use. Default is ['.js', '.jsx']

doctype: the <!DOCTYPE> you wish to be set. Default is <!DOCTYPE html>

#Warning In large systems the first page view will be slow because babel is parsing it. After the view is cached in requre.cache the page renders faster. If you run multiple instances of express and load-manage, please force a page load after deploy on each to initially build.

react-view-engine's People

Contributors

jgimbel avatar malachi-m avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

liveby

react-view-engine's Issues

ReferenceError: window is not defined

Could you help me figure out my issue? When I try and render my view, which is actually the error view, I am getting the error "window is not defined", but I am using your example view.

here is the error:

ReferenceError: window is not defined
   at Object.<anonymous> (client\views\error.js:25:3)
   at Module._compile (module.js:456:26)
   at Object.require.extensions.(anonymous function) [as .js] (node_modules\node-jsx\index.js:26:12)
   at Module.load (module.js:356:32)
   at Function.Module._load (module.js:312:12)
   at Module.require (module.js:364:17)
   at require (module.js:380:17)
   at View.module.exports.loader.engine (node_modules\react-view-engine\index.js:20:22)
   at View.render (node_modules\express\lib\view.js:93:8)
   at EventEmitter.app.render (node_modules\express\lib\application.js:566:10)

I am new to react, and this is a new project setup, I very well may of done something wrong, so any insight is appreciated.

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.