Skip to content

Instantly share code, notes, and snippets.

@danott
Created October 5, 2019 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danott/6fadcb5ac8dba6ec539f8bdafceaa123 to your computer and use it in GitHub Desktop.
Save danott/6fadcb5ac8dba6ec539f8bdafceaa123 to your computer and use it in GitHub Desktop.
A minimal Rails helper for rendering a React component into the DOM
module ReactHelper
def render_react_component(component:, props: {}, dom_id:)
<<-REACT_DOM_RENDER.strip_heredoc.squish.html_safe
ReactDOM.render(
React.createElement(#{sanitize(component)}, #{raw json_escape(props.to_json)}),
document.getElementById("#{sanitize(dom_id)}")
);
REACT_DOM_RENDER
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment