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.
Please use the https://github.com/shakacode/react_on_rails/ gem. We've already got an example of this here: https://github.com/shakacode/react-webpack-rails-tutorial/. Please see the recently accepted PRs. We're very actively developing this technology. My new company specializes in react + redux + react-router + rails. Please post your questions to http://forum.railsonmaui.com. We've got a slack room as well for discussing this.