Skip to content

Conversation

@rmosolgo
Copy link
Member

@rmosolgo rmosolgo commented Jun 28, 2016

Currently, a renderer is "checked out" for each call to react_component. However, if you want some state in that context, it's inefficient: you have to load the state before each call, then clear it after each call.

This PR adds support for one-renderer-per-request, so you can load the JS state before rendering the view, then unload it afterwards.

You could do like this:

class ThingsController < ApplicationController 
  # This around-action is provided by react-rails: 
  per_request_react_renderer

  def show 
     # `react_rails_prerenderer` is assigned by react-rails 
     js_context = react_rails_prerenderer.context 
     js_context.eval(setup_code)
     render :show 
     js_context.eval(teardown_code)
  end 
end 

Of course, you could further generalize this solution with before_/after_/around_actions

Todo

  • End-to-end test using __prerenderer.context
  • Improve the API?

@rmosolgo rmosolgo force-pushed the per-request-js-ctx branch from 727a8d6 to 841da3a Compare April 5, 2017 14:28
@rmosolgo rmosolgo force-pushed the per-request-js-ctx branch from 841da3a to f7544d3 Compare April 5, 2017 15:51
@rmosolgo rmosolgo merged commit 79101ff into 2.0-dev Apr 5, 2017
@rmosolgo rmosolgo deleted the per-request-js-ctx branch April 5, 2017 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants