Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react gets stuck after i resize the browser window

I used react-simple-image-slider for image slider on my website, but after i resize the browser window the application gets stucked.

The main Aim is to make the react-simple-image-slider responsive.

The below is the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import './App.css';
import {useLayoutEffect, useState } from 'react';
import SimpleImageSlider from 'react-simple-image-slider';

function App() { 
  const [widthAndHeight, setwidthAndHeight] = useState([window.innerWidth, 600]);
  
  useLayoutEffect ( ()=>
  {
    function updateTheWidthAndHeight()
    {
      setwidthAndHeight([window.innerWidth, 600]);
      
    }
      window.addEventListener('resize', ()=>{
      updateTheWidthAndHeight();
    })
  })

  const images = [
    { url: "logos/ethiopia.png" },
    { url: "logos/favicon.ico" },
    { url: "logos/gear.png" },
    { url: "logos/gear.jpg" }
  ];


  return (
      <div>
            <SimpleImageSlider
                style={{ margin: '0 auto', marginTop: '0px'}}
                className="slider"
                width={widthAndHeight[0]}
                height={widthAndHeight[1]}
                slideDuration={1}
                loop={true}
                autoPlay={true}
                images={images}
                showBullets={false}
                showNavs={true}
            />
      </div>
  );
}

export default App;

>Solution :

You forgot a dependency list for useLayoutEffect so it’s adding an event listener on every render. Add an empty list for one-time execution:

useLayoutEffect(() => {
  function updateTheWidthAndHeight() {
    setwidthAndHeight([window.innerWidth, 600]);
  }
  window.addEventListener("resize", () => {
    updateTheWidthAndHeight();
  });
}, []);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading