Skip to content

Commit 1e7ddf2

Browse files
author
Nicholas Barone
committed
add support for passing children in from Rails
1 parent bb9a8a2 commit 1e7ddf2

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/react_on_rails/helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ module Helper
5353
# Any other options are passed to the content tag, including the id.
5454
# random_dom_id can be set to override the default from the config/initializers. That's only
5555
# used if you have multiple instance of the same component on the Rails view.
56-
def react_component(component_name, options = {})
56+
def react_component(component_name, options = {}, &block)
57+
(options[:props] ||= {})[:children_html] = capture(&block) if block
58+
5759
internal_result = internal_react_component(component_name, options)
5860
server_rendered_html = internal_result[:result]["html"]
5961
console_script = internal_result[:result]["consoleReplayScript"]

node_package/src/createReactOutput.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export default function createReactOutput({
2626
}: CreateParams): CreateReactOutputResult {
2727
const { name, component, renderFunction } = componentObj;
2828

29+
const children = props?.children_html ? React.createElement('div', {dangerouslySetInnerHTML: {__html: props.children_html }}) : null;
30+
2931
if (trace) {
3032
if (railsContext && railsContext.serverSide) {
3133
console.log(`RENDERED ${name} to dom node with id: ${domNodeId}`);
@@ -68,8 +70,8 @@ work if you return JSX. Update by wrapping the result JSX of ${name} in a fat ar
6870

6971
// If a component, then wrap in an element
7072
const reactComponent = renderFunctionResult as ReactComponent;
71-
return React.createElement(reactComponent, props);
73+
return React.createElement(reactComponent, props, children);
7274
}
7375
// else
74-
return React.createElement(component as ReactComponent, props);
76+
return React.createElement(component as ReactComponent, props, children);
7577
}

0 commit comments

Comments
 (0)