koajs / react-view

A Koa view engine which renders React components on server
259 stars 30 forks source link

support koa2? #22

Open hskww opened 8 years ago

icflorescu commented 7 years ago

Apparently it does support koa@2.

I've just tested a very simple workflow with koa-react-view@3.0.0, koa@2.0.0-alpha.7:

// bootstrap.js:

require('babel-core/register')({ presets: ['latest-minimal', 'react'] });
require('./app');

Start server:

// app.js:

import Koa             from 'koa';
import setupViewEngine from 'koa-react-view';

const app = new Koa();

setupViewEngine(app, {
  views: `${__dirname}/views`,
  extname: 'js'
});

app.use(async ctx => {
  ctx.state = {
    title: 'Awesome app',
    viewEngine: 'React'
  };
  await ctx.render('home');
});

app.listen(3000);

This works as expected and it renders both ES6 classes:

// views/home.js:

import React, { Component } from 'react';

export default class Home extends Component {
  render() {
    return (
      <html>
        <head>
          <title>{this.props.title}</title>
        </head>
        <body>
          <h1>{this.props.title}</h1>
          <p className="test">Page rendered with {this.props.viewEngine} view engine.</p>
        </body>
      </html>
    );
  }
};

...and stateless functional components:

// views/home.js:

import React from 'react';

export default props => (
  <html>
    <head>
      <title>{props.title}</title>
    </head>
    <body>
      <h1>{props.title}</h1>
      <p className="test">Page rendered with {props.viewEngine} view engine.</p>
    </body>
  </html>
);

I'm just curious, did you encounter a specific use-case where this engine didn't work with koa@2?

afenton90 commented 7 years ago

I have also managed to get this working with koa@2 and koa-react-view@3.0.0.