React with rails issue
I am newby in rails and react project. I have installed all the required gems of react in rails project. when i try to render the request like this
ReactDOM.render(
it shows me the error like
Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.
Hi, welcome!
I have a couple suggestions.
-
Instead of rendering components with
ReactDOM.render, how about using thereact_componentview helper which is provided byreact-rails? To use it, do this:-
Write your React component in
/app/assets/javascripts/components/ -
Require your component in
application.jswith//= require components/my_component -
In the view, use the
react_componenthelper with the component's name & props. For example,<%= react_component("MyComponent", {my_prop: "my_value"}) %>
-
-
In your example, there first argument to
ReactDOM.renderis missing. Where is the component which you want to render? -
Make sure the JavaScript runs after
#documenthas been rendered. For example, this does not work:<script>document.getElementById("container")</script> <div id="container"></div>but this does work:
<div id="container"></div> <script>document.getElementById("container")</script>If the JS runs first, it won't find the
<div>because it has not been rendered yet!
Can two different components rendered through react_component() share state ? Would one re-render if the other changes the state?
If you're still looking for a way to communicate between unrelated components, have a look at Flux :)