Skip to content

Commit

Permalink
add support for passing children in from Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Barone committed Oct 1, 2024
1 parent bb9a8a2 commit 1e7ddf2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/react_on_rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ module Helper
# Any other options are passed to the content tag, including the id.
# random_dom_id can be set to override the default from the config/initializers. That's only
# used if you have multiple instance of the same component on the Rails view.
def react_component(component_name, options = {})
def react_component(component_name, options = {}, &block)
(options[:props] ||= {})[:children_html] = capture(&block) if block

internal_result = internal_react_component(component_name, options)
server_rendered_html = internal_result[:result]["html"]
console_script = internal_result[:result]["consoleReplayScript"]
Expand Down
6 changes: 4 additions & 2 deletions node_package/src/createReactOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default function createReactOutput({
}: CreateParams): CreateReactOutputResult {
const { name, component, renderFunction } = componentObj;

const children = props?.children_html ? React.createElement('div', {dangerouslySetInnerHTML: {__html: props.children_html }}) : null;

if (trace) {
if (railsContext && railsContext.serverSide) {
console.log(`RENDERED ${name} to dom node with id: ${domNodeId}`);
Expand Down Expand Up @@ -68,8 +70,8 @@ work if you return JSX. Update by wrapping the result JSX of ${name} in a fat ar

// If a component, then wrap in an element
const reactComponent = renderFunctionResult as ReactComponent;
return React.createElement(reactComponent, props);
return React.createElement(reactComponent, props, children);
}
// else
return React.createElement(component as ReactComponent, props);
return React.createElement(component as ReactComponent, props, children);
}

0 comments on commit 1e7ddf2

Please sign in to comment.