I installed react-rails on my project, added the
gem 'react-rails'
then I used
rails g react:install
I have my index html index.html.erb with the react_component helper calling my componnet
<%= react_component("Post", {title: "Hello World"}) %>
And I have this post component at app/assets/javascript/components/post.jsx
class Post extends React.Component {
render() {
return <h1>{this.props.title}</h1>
}
}
But my component is not rendering on the screen
why?
There are two common reasons why React might not update a component even though its props have changed: The props weren't updated correctly via setState. The reference to the prop stayed the same.
check your console for errors chances are high it's not showing any and if it does , try to trace the back to the line of bug that was thrown and fix it. There's probably an infinite loop running somewhere in your code.
Forcing an update on a React class component In any user or system event, you can call the method this. forceUpdate() , which will cause render() to be called on the component, skipping shouldComponentUpdate() , and thus, forcing React to re-evaluate the Virtual DOM and DOM state.
The error "ReactDOM. render is no longer supported in React 18. Use createRoot instead" occurs because the ReactDOM. render method has been deprecated.
Make sure you have added the <%= javascript_pack_tag 'application' %>
directive into your layout file.
Also in my case I forgot to run rails generate react:install
as specified here: https://github.com/reactjs/react-rails#3-now-run-the-installers which sets up the ReactRailsUJS setup in app/javascript/packs/application.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With