-1

I made a small app using react and it works smoothly on my computer. However, when I open it on different mobile devices it crashes after a minute or so of usage. I don't know what causes the problem but weirdly enough I don't have any bugs or warnings on my console and haven't used componentDidUpdate or hooks that might cause infinite loops like useEffect. Please help me if you know what causes a react app to glitch on mobile devices or have encountered the same problem and found a solution for it. Here's the app

https://summerresort.netlify.app

and the code

https://github.com/Mahmoud-farargy/summerresort/tree/master/Beach-Resort

Thanks!

S.Visser
  • 4,645
  • 1
  • 22
  • 43

1 Answers1

1

Your app looks all ok except 2 points. I can't test my guess, but I'm almost entirely sure that your problem comes from those 2.

Both parts that seem problematic are located in index.js, here there are:

window.addEventListener("scroll", () => {// gets the current height
    let  scroll = document.body.scrollTop || document.documentElement.scrollTop;
    let arrowUp = document.querySelector(".arrow-up-btn").style;
    scroll >= 1000 ? arrowUp.display="flex" : arrowUp.display = "none";
});

window.addEventListener("resize", ()=> {// gets the current width
    this.setState({
        currentPageWidth: window.innerWidth || document.documentElement.clientWidth
    })
});

The first code block is expensive because:

  1. The scroll event is fired very frequently.
  2. You don't listen for scroll passively (it'd be a good idea though).
  3. You set the style much more frequently than needed.

However, I think, that it is the second code block that causes mobiles to glitch.
Check out this answer to see why the resize event is fired on mobiles more frequently than on desktop.

  1. Now, each time the resize event is triggered, the state of MainApp is updated.
  2. Each time MainApp is updated, React recalculates all its children.
  3. By default, React just re-run render of each child & compares the output.

Considering you have all routes & all components inside the MainApp & you didn't implement any optimizations (React.PureComponent, lazy loading for routes, etc.) React will have to recalculate what the entire site looks like on each resize event.

The easiest fix would be to store currentPageWidth in a ref instead of state.

Another possible problem is that by attaching listeners inside the render method, you're attaching new listeners on each single render without clearing any of previously added listeners.

Igor Bykov
  • 2,532
  • 3
  • 11
  • 19
  • Yeah, dude, I figured that out a while ago the main problem was with the resize event which caused the app to clitch while not throwing an error after I removed it the app runs very smoothly. So moral of the story, don't use the resize event except when it necessary and always keep a copy of your app on another place or use Git so whenever you encounter a problem with your app you'll find old versions to fall back into. – Mahmoud Farargy Aug 31 '20 at 23:38
  • And you were right about React.PureComponent. I should have used it to block most components from re-rendering unnecessarily. – Mahmoud Farargy Aug 31 '20 at 23:41