How do I dynamically create a React component from javascript in Rails/react-rails?

286 Views Asked by At

I normally create a React component in Rails server rendered pages using the standard react_component helper in react-rails, e.g.

 react_component("thing/MyComponent", { company_id: @company.id })

This will create the component at app/javascript/components/thing/MyComponent.js

Now I have a requirement to create the component dynamically as the user interacts with the ERB page by clicking on a link.

How would I create the same React component from a vanilla javascript method embedded on the page?

Here's an outline of what I am after:

<script>
  function showThing(id) {
     // What code do i need to put here to create a new thing/MyComponent component in the #target div?
     // This solution can use jQuery that is available here
  }
</script>
<a onClick="showThing(10)">Click</a>
<div id="target"/>

Any help is appreciated. Thanks

0

There are 0 best solutions below