GithubHelp home page GithubHelp logo

express-react-engine's Introduction

express-react-engine

This is an Express view engine for React's JSX that supports server side rendering and it is not limited to static markdown.

Usage

npm install express-react-engine react react-dom

Make sure you install react and react-dom as dependencies.

Add it to your Express App

var ReactEngine = require('express-react-engine');
var app = express();

app.set('views', __dirname + '/components');
app.engine('jsx', ReactEngine());

Change your views directory to match your components directory and set jsx as your view engine.

Options

wrapper is a React component that renders the Html element as well as the initial props and children Html.

Example

app.engine('jsx', reactEngine({wrapper: 'html.jsx'}));

/components/html.jsx

var React = require('react');

var Html = React.createClass({
  render: function () {
    return (
      <html>
        <head>
          <title>{this.props.props.title}</title>
          <link rel='stylesheet' type='text/css' href='/stylesheets/style.css' />
        </head>
        <body>
            <div id='view' dangerouslySetInnerHTML={{__html: this.props.body}} />
            <script type='application/json' dangerouslySetInnerHTML={{__html: JSON.stringify(this.props.props)}} />
            <script src='/javascripts/bundle.js' />
        </body>
      </html>
    );
  }
});

module.exports = Html;

Views

Your views can be simple modules that export a React Component.

var React = require('react');

var Home = React.createClass({
  render: function () {
    return (
      <div>
        E.T. Phone Home!
      </div>
    );
  }
});

module.exports = Home;

Routes

You can use your routes the same way you always did in Express. Just render the React component instead of a jade/hjs/hbs template. Passing an object to the component as props is also straightforward, just add it as the second argument of res.render().

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
  res.render('home/index.jsx', { foo: 'bar' });
});

module.exports = router;

Now foo will be available within the component as this.props.foo.

Layouts

Obviously you can still use Layouts if you want. Just create a component that will render the "layout stuff" and pass him via props the component that you want to be laid out :)

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.