Giter Club home page Giter Club logo

koa2-react-view's Introduction

koa2-react-view

React SSR with Koa 2

Installation

With NPM

npm install koa2-react-view

Or with Yarn

yarn add koa2-react-view

Usage

Server file

const Koa = require('koa')
const render = require('koa2-react-view')
/* Or
import Koa from 'koa'
import { render } from 'koa2-react-view'
*/

const app = new Koa()
// Register middleware
app.use(
  render({
    root: path.join(__dirname, 'views'),
    ext: 'jsx',
    cache: false,
    layout: 'Layout',
  })
)

// Use ctx.render to render a view with variables
app.use(async (ctx) => {
  return ctx.render('home', { name: 'World' })
})

app.listen(3000)

View file

import React from 'react'

// Views (as well as layout) should always be default exported
export default function Home({ name }) {
  return <div>Hello {name}!</div>
}

Variables

Are available in the view:

  • all the variables defined during ctx.render process
  • ctx.state, through the __state prop

Options

name type mandatory default value description
root string yes root folder of the views
ext string no jsx extension of the view (with or without a .)
cache boolean no true should the views be cached or not (recommended: true for production, false for development)
layout string no filename of the layout component views

Using a layout

You can use a layout to wrap your views. All variables available in the view are available in the layout.

import React from 'react'

export default function Layout({ children }) {
  return (
    <html>
      <head></head>
      {/* Bellow is your view */}
      <body>{children}</body>
    </html>
  )
}

koa2-react-view's People

Contributors

saramorillon avatar

Watchers

 avatar

Forkers

ittp

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.