question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReactRailsUJS: how to stop listening for events?

See original GitHub issue

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.
  • 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.

Hello there!

Thank you for the awesome gem! I want to implement ‘lazy’ loading for my react components and render them after window.onload event fired. But ReactRailsUJS start watching for turbolinks events and render components during loading page (on turbolinks:load). The page starts waiting and fires window.onload event only after all react components rendered.

How I can stop listening for events when ReactRailsUJS loaded? (I want to mountComponents manually on first load and detectEvents afterwords).

Let me know if something is unclear in my description 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
anaumovcommented, Nov 29, 2017

hey, everyone! If someone looking for solution, it’s pretty easy ReactRailsUJS has method removeEvent. You can stop listening for any event you like. For example render components on window load instead of turbolinks:render

ReactRailsUJS.removeEvent('turbolinks:render', ReactRailsUJS.handleMount);
ReactRailsUJS.removeEvent('DOMContentLoaded', ReactRailsUJS.handleMount);
$(window).on('load', function() {
  ReactRailsUJS.handleMount();
  ReactRailsUJS.detectEvents();
})
1reaction
BookOfGregcommented, Nov 29, 2017

Thanks @anaumov ! I’ve added an entry to the Wiki with your example: https://github.com/reactjs/react-rails/wiki/How-to:-Stop-listening-to-events

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to: Stop listening to events - reactjs/react-rails GitHub Wiki
Based on discussion here: #805. If you want to stop listening to a specified event, it's possible to name the events to stop...
Read more >
Stop event listening after route change - Stack Overflow
The solution I found consists in removing the event listener before unmounting the component. However, to do that I need to keep a...
Read more >
Project: react-rails - The Ruby Toolbox
listen. ~> 3.0.0 ... Prevent installing default javascript dependencies by using ... Remove previous event handlers and add new ones: ReactRailsUJS.
Read more >
How to (really) remove eventListeners in React
Sometimes you need to track user interaction like e.g. scrolling or events like the change of the win... Tagged with javascript, react, ...
Read more >
Redux and React :: Full-stack web development "Hello World ...
... this allows Redux to do interesting things like keep an accurate ... to send event actions into a set of reducers that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found