Git Product home page Git Product logo

node-horse-react's Introduction

horse-react

Build Status

horse-react is an implementation of the horse application building library, with some helpers to make working with react easily.

Go check out that documentation, then return to see how you'd use it with React a little easier.

New APIs

horse-react exposes pre-built render and error functions that you can hook into, through ClientApp and ServerApp classes. It expects your middleware to attach a layout, body, and props property to the context object during the course of your route handling, and at the end, it will render it out (either with layout, if on the server, or it will mount the body if on the client.)

A Brief Overview

An example usage might be like: (es6 incoming)

routes.es6.js

// This is used both client- and server- side, and simply sets up an app with
// routes; in this case, returning React elements.

import Layout from '../layouts/layout.jsx';
import Index from '../pages/index.jsx';

function setupRoutes(app) {
  app.router.get('/', function *(next) {
    this.data = new Map({
      user: db.getUser(1)
    });

    this.layout = Layout;

    this.body = function(props) {
      return <Index {...this.props} />;
    });
  });
}

export default setupRoutes;

server.es6.js

import koa from 'koa';

import {ServerReactApp} from 'horse-react';
import setupRoutes from './setupRoutes';

var server = koa();

var app = new App();
setupRoutes(app);

server.use(ServerReactApp.serverRender(app));

client.es6.js

You'll want to add push state too, but that's outside the scope of our example.

import React from 'react';
import {ClientReactApp} from 'horse-react';

import setupRoutes from './setupRoutes';

import jQuery as $ from 'jquery';

var app = new ClientApp({
  mountPoint: document.getElementById('app-container')
});

setupRoutes(app);

$(function() {
  $('body').on('click', 'a', function(e) {
    e.preventDefault();
    app.render(this.href);
  });
});

Additional Notes

If you want to mount a client application directly on the server-rendered markup, add this.staticMarkup to the context before serverRender is called. Your layout should include !!CONTENT!! as the magic word where rendered body markup should be inserted (instead of {this.children}.)

node-horse-react's People

Contributors

ajacksified 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.