You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can extend some of the core functionality of `react-rails` by injecting new implementations during configuration.
313
+
314
+
### Custom Server Renderer
315
+
316
+
`react-rails` depends on a renderer class for rendering components on the server. You can provide a custom renderer class to `config.react.server_renderer`. The class must implement:
317
+
318
+
-`#initialize(options={})`, which accepts the hash from `config.react.server_renderer_options`
319
+
-`#render(component_name, props, prerender_options)` to return a string of HTML
320
+
321
+
`react-rails` provides two renderer classes: `React::ServerRendering::ExecJSRenderer` and `React::ServerRendering::SprocketsRenderer`.
322
+
323
+
`ExecJSRenderer` offers two other points for extension:
324
+
325
+
-`#before_render(component_name, props, prerender_options)` to return a string of JavaScript to execute _before_ calling `React.render`
326
+
-`#after_render(component_name, props, prerender_options)` to return a string of JavaScript to execute _after_ calling `React.render`
327
+
328
+
Any subclass of `ExecJSRenderer` may use those hooks (for example, `SprocketsRenderer` uses them to handle `console.*` on the server).
329
+
330
+
### Custom View Helper
331
+
332
+
`react-rails` uses a "helper implementation" class to generate the output of the `react_component` helper. The helper is initialized once per request and used for each `react_component` call during that request. You can provide a custom helper class to `config.react.view_helper_implementation`. The class must implement:
333
+
334
+
-`#react_component(name, props = {}, options = {}, &block)` to return a string to inject into the Rails view
335
+
-`#setup(rack_env)`, called when the helper is initialized at the start of the request
336
+
-`#teardown(rack_env)`, called at the end of the request
337
+
338
+
`react-rails` provides one implementation, `React::Rails::ComponentMount`.
339
+
340
+
### Custom JSX Transformer
341
+
342
+
`react-rails` uses a transformer class to transform JSX for the browser. The transformer is initialized once, at boot. You can provide a custom transformer to `config.react.jsx_transformer_class`. The transformer must implement:
343
+
344
+
-`#initialize(options)`, where options is the value passed to `config.react.jsx_transform_options`
345
+
-`#transform(code_string)` to return a string of transformed code
346
+
347
+
`react-rails` provides two transformers, `React::JSX::JSXTransformer` and `React::JSX::BabelTransformer`.
0 commit comments