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
Rails ships with a useful JSON builder that makes rendering to strings
of JSON via templates very easy, but rendering from objects to json
not quite so easy. It's used by a lot of Rails apps as the default way
to serialize objects to JSON.
This allows render_component to take a string as the props argument,
and instead of calling #to_json on it, just pass it straight through
to the renderer.
Basically, this allows syntax like the following:
```ruby
react_component(
'Todo',
render(template: 'todos/show.json.jbuilder', locals: {todo: @todo}),
{prerender: true}
)
```
This is not suitable for ReactJS props, which is expected to be a key-value object. You will need to wrap your index.json.jbuilder node with a root node, like so:
React components can also use the same ExecJS mechanisms in Sprockets to execute JavaScript code on the server, and render React components to HTML to be delivered to the browser, and then the `react_ujs` script will cause the component to be mounted. In this way, users get fast initial page loads and search-engine-friendly pages.
test'Rendering does not throw an exception when console log api is used'do
12
22
%W(errorinfologwarn).eachdo |fn|
13
23
assert_nothing_raised(ExecJS::ProgramError)do
@@ -23,4 +33,17 @@ class ReactRendererTest < ActiveSupport::TestCase
23
33
expected_message='Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
24
34
assert_equalexpected_message,err.message
25
35
end
36
+
37
+
test'prerender errors are thrown when given a string'do
expected_message='Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
0 commit comments