The main issue I found when integrating react-rails and flux/redux is managing application state. In redux I'd like to have a single application state serialized from the server and parsed in the client, and to use that state to initialize the main store. Something like:
initialState = JSON.parse(window.__APPSTATE__);
But that won't work since react-rails splits its work to 2 phases: it first reads all .js files in the renderer initialize phase and every call to the view helper only calls render.
Solution is described below by using a new renderer that:
- takes an @appstate attribute in its prerender options
- uses the before_render hook to inject JS code before calling render. That code dispatches a new action setting the application state.
- only then it calls the component's render function
I'm not sure it's the best solution or how to generalize it, so any feedback is welcomed.
thanks a lot for the gist @ynonp I used it as a base for https://gist.github.com/jhilden/44378421ad54e617b900
one question. shouldnt the output of
super
inside thebefore_render
method also be returned together with the new output?like:
super + "if ( ..."