Skip to content

Instantly share code, notes, and snippets.

@nambrot
Created April 13, 2015 13:57
Show Gist options
  • Save nambrot/6db6304068ddbe3ce6dc to your computer and use it in GitHub Desktop.
Save nambrot/6db6304068ddbe3ce6dc to your computer and use it in GitHub Desktop.
# server.coffee
renderToString = (route, serializedStoreState, callback) ->
flux = new Flux()
flux.deserialize(serializedStoreState)
Router.run routes, route, (Handler, state) ->
html = React.renderToString(<FluxComponent flux={flux} render={ => <Handler />}></FluxComponent>)
embeddedStoreState = "<script>"
embeddedStoreState += "window.serializedStoreState = " + JSON.stringify(serializedStoreState)
embeddedStoreState += "</script>"
callback (html + embeddedStoreState)
# app/helpers/react_helper.rb
module ReactHelper
def renderSerializedStoreState(state)
component = HTTParty.post "http://localhost:3001", body: {path: request.path, serializedStoreState: state.to_json}.to_json, headers: {'Content-Type' => "application/json"}
component.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment