writing koa middleware for fluxible-router (async executeAction)

133 views Asked by At

I'm trying to use koa with fluxible-router. Most express examples use a similar pattern but then use send() directly into the navigateAction.

React.renderToString() get the right props just hen called in executeAction context.

'use strict';

var React = require('react');
var app = require('./app');
var navigateAction = require('fluxible-router').navigateAction;

function render() {
  return function *render(next) {
    var context = app.createContext(
      {
        req: this.request
        , xhrContext: {
          _csrf: 'pino'
        }
      }
    );
    var html = null;
    context.executeAction(navigateAction, {
        method: this.request.method
        , url: this.request.url
    }, function (err) { // this function is executed asyncronously
      console.log('1');
      var html = React.renderToString(context.createElement());
    });
    this.body = html;
    console.log('2');
    yield* next;
  }
}

module.exports = render;

The only way I've found to get props populated was to run renderToString() inside executeAction() but it get executed asyncronously, so that console.log('2') get executed before console.log('1') and the returned body is null.

I'm not aware of any way to wait for renderToString nor any way to run renderToString() in the right context.

thanks

1

There are 1 answers

0
HumbleProgrammer On

this is a way to do it

https://gist.github.com/cesarandreu/03f6c584a40c62812deb

or just yield context.executeAction(...)