Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react component not rendering on my view

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?

like image 413
Victor Oliveira Avatar asked Apr 24 '17 17:04

Victor Oliveira


People also ask

Why my component is not rendering in React?

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.

Why is React not displaying anything?

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.

How do I force a component to render?

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.

Why ReactDOM render is not working?

The error "ReactDOM. render is no longer supported in React 18. Use createRoot instead" occurs because the ReactDOM. render method has been deprecated.


1 Answers

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

like image 136
Constantine Avatar answered Oct 23 '22 16:10

Constantine