Serverside rendering in koa made easy
npm install koa-redux```
npm install koa-redux
Add koa-redux to your koa application's context:
`js
var koa = require('koa')
var koaRedux = require('koa-redux')
var app = koa()
koaRedux(app) // app = koaRedux(app) is equivalent`
Now in your routes/middleware you can access the redux property on koa's this context.
`js
var myReducer = require('./path/to/my/reducer')
var myComponentFn = require('./path/to/my/componentFn')
app.use(function * (next) {
var initialState = {text: 'hello world'} // initial state of the redux store
var renderObj = this.redux.handleRender(componentFn, reducer, initialState)
var html = this.redux.getHtml(renderObj)
this.body =
})
`API
The function exported by koa-redux adds a
redux property to koa's this context. The redux property is an object containing two functions:$3
handleRender takes four arguments:1.
componentFn—A function which takes a redux store as its argument and returns the root react component.
2. reducer—The root reducer function for the redux application.
3. initialState—The initial state provided to the root reducer. Defaults to {} if undefined.
4. compArgs—Additional arguments passed to the componentFnhandleRender returns an object with two properties:1.
html—The rendered html of the react components.
2. state—The state used to render the html.$3
Takes an object returned by
handleRender and wraps it in an html string that can be used directly when rendering the response body. The object's html property is wrapped in a