rendering - Isomorphically render html string with Koa -
i'm trying koa isomorphically render html string receives react-router.
here's code i've been trying make work:
server.js
import koa 'koa'; import react "react"; import router "react-router"; import routes "./routes"; const server = koa(); const port = process.env.node_env || 8000; server.use(function *() { try { yield router.run(routes, this.request.url, function (handler) { var content = react.rendertostring(<handler/>) this.body = content }) } catch (err) { this.status = err.status || 500; this.body = err.message; this.app.emit('error', err, this); } }) server.listen(port, function(err) { if (err) throw err; console.log("server running on: http://localhost:"+port) })
routes.js
import react "react" import {route, defaultroute} "react-router" import main "./components/main" export default ( <route path="/"> <defaultroute handler={main} name="main" /> </route> )
main.js
import react "react" const main = react.createfactory(react.createclass ({ render () { return ( <div>hello</div> ) } })) export default main
getting several errors:
warning: component(...): no
render
method found on returned component instance: may have forgotten definerender
in component or may have accidentally tried render element type function isn't react component.warning: don't set props property of react element. instead, specify correct value when creating element.
typeerror: can't add property context, object not extensible
warning: calling react component directly. use factory or jsx instead. see: https://fb.me/react-legacyfactory
for needs answer this, hope guys.
server.js
import koa 'koa'; import react "react"; import router "react-router"; import routes "./routes"; const server = koa(); let handler; server.use(function *(next) { router.run(routes, this.request.url, (handler) => { handler = react.rendertostring(<handler/>); }); yield next; }); server.use(function *() { this.body = handler; }); export default server;
components/index.js
import react "react"; const main = react.createclass ({ render () { return ( <div>hello</div> ); } }); export default main;
i dont see need create factory one.
Comments
Post a Comment