react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

react_component helper not rendering component

Open chrismv48 opened this issue 9 years ago • 11 comments

Help us help you! Please choose one:

  • [ ] My app crashes with react-rails, so I've included the stack trace and the exact steps which make it crash.
  • [x] My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • [ ] I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.
  • [ ] I have another issue to discuss.

I've followed the instructions in the readme to setup react-rails with webpacker and am trying to get a component, HelloWorld.js to render using <%= react_component("HelloWorld") %> in my application layout view. The helper properly converts to this div: <div data-react-class="HelloWorld" data-react-props="{}"></div> but the corresponding component is not rendered.

I have a feeling this issue is related to my other issue here.

My project is available on github here.

chrismv48 avatar May 20 '17 00:05 chrismv48

After making a pack with webpacker, you must include it with a <script> tag:

image

Since the pack wasn't included, ReactRailsUJS wasn't on the page. ReactRailsUJS is required to mount components.

After adding that line, it worked for me:

image

Please reopen if that doesn't work for you!

rmosolgo avatar May 20 '17 14:05 rmosolgo

I'm quite new to implementing React into a Rails app (been working on it for 1 day so far, lol) but what I notice is that when providing { prerender: true }, the component does render on the page, but I can't seem to get an onChange event to fire in an input element. Is this because the component has been server-side rendered and thus the magic client-side rendering of React is mitigated? Any help is greatly appreciated!

For more context, I've included:

  • <%= javascript_pack_tag 'application' %> in my application.html.erb layout file
  • <%= react_component("ComponentName", { props: @props }) %> in my view file

dsuare1 avatar Jan 30 '18 21:01 dsuare1

@dsuare1 You may want to ask on Stack Overflow for these kinds of help questions, or open a new issue as it's quite easy for comments to get lost in closed issues.

For now though let's take a look. Server-side rendering doesn't affect client-side rendering as the components are rendered client-side anyway so there should not be a problem with that.

  1. Are there any issues in the console?
  2. Does it work if you do not prerender?
  3. Are there any other JS libraries that may affect it such as Turbolinks or JQuery (Especially JQuery3 as there is a known bug with it)?

BookOfGreg avatar Jan 31 '18 09:01 BookOfGreg

Thanks for your response @BookOfGreg; there are a few related questions on StackOverflow, but none of them helped me arrive at a solution.

  1. There are no issues in the console that should be related to this problem.
  2. It does not work if I don't prerender. Well, it's strange because when I don't pass { prerender: true }, I can see the React Component inserted into the DOM, but whatever content is inside the Component does not render to the page, and is not present in the DOM. *See further explanation below
  3. Turbolinks and jquery-rails as well jquery-ui-rails libraries are included, so that may be causing an issue

*To further explain Point 2 above, if I have a react component structured like: class HelloWorld extends React.Component { render() { return ( <div> <h1>Hello, World</h1> </div> ) } I can see that there is a <div> rendered to the page with data-react-class=HelloWorld and data-react-props={} attributes, but that inner <h1> content is not included as part of the

nor does it show up on the page.

dsuare1 avatar Jan 31 '18 15:01 dsuare1

Continuing on a hunch, can you run ReactRailsUJS.mountComponents() in your console? I suspect it's JQuery3 breaking the automatic mounting again. If so then it's just #762

BookOfGreg avatar Jan 31 '18 15:01 BookOfGreg

@BookOfGreg Yes, calling ReactRailsUJS.mountComponents() manually in the console, once the page has loaded, does work and results in the component rendering. But I've read through 762 and the solution in there doesn't seem to fit my issue exactly. Deekor mentions moving the //= require components below the //= require jquery3, but I don't see a //= require components statement in my app/assets/javascripts/application.js file. Am I missing something in there?

Edit: I added the ReactRailsUJS.mountComponents() manual call as the last line in my app/javascript/packs/application.js file and that resulted in the component being mounted and rendered automatically!

Wondering if this is what we exactly want though...or is it a hacky solution?

dsuare1 avatar Jan 31 '18 18:01 dsuare1

It's a hacky solution for now but it is safe to do it that way. You will likely be using JQuery 3 and it broke it's support, so someone needs to update the file at https://github.com/reactjs/react-rails/blob/master/react_ujs/src/events/detect.js to play nicely with it.

BookOfGreg avatar Feb 02 '18 12:02 BookOfGreg

I also want to know the answer to this question...

buck3000 avatar Feb 02 '18 16:02 buck3000

@BookOfGreg Thanks for the help! You helped me get unblocked and keep my development moving forward. I'll keep an eye out for when / if this fix comes through, but for now, adding the manual call to ReactRailsUJS.mountComponents in app/javascript/packs/application.js works!

dsuare1 avatar Feb 02 '18 16:02 dsuare1

Mine is still not working even when I paste ReactRailsUJS.mountComponents() at the end of my app/javascript/packs/application.js - has anyone else had any luck doing anything else?

kaylalarson1990 avatar Nov 26 '19 15:11 kaylalarson1990

no lucky here too so, my hacky solution... in the view file where I call <%= react_component(... I just placed on the bottom:

<script>
  $(document).ready(function() { ReactRailsUJS.mountComponents(); })
</script>

jmmalaca avatar Feb 18 '20 18:02 jmmalaca