最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Resize event in react - Stack Overflow

matteradmin7PV0评论

I want to listen for window resize event in react.js. I tried using window.addEventListener but that fires the callback multiple times. Is there a way that the callback runs only once or simply a more react oriented way(a hook maybe) to achieve this?

    const resizeQuery = () => {
    console.log("check");
    if (desktop) setnumberOfShownImages(4);
    else if (tablet) setnumberOfShownImages(3);
    else if (mobile) setnumberOfShownImages(1);
  };

    window.addEventListener('resize',resizeQuery);

full Component code:

function ImageSlider(props) {
  const [numberOfShownImages, setnumberOfShownImages] = useState(props.numberOfShownImages);
  const currentIndex = 0;
  const array = [
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 1"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 2"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 3"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 4"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 5"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 6"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 7"/>,
  ];
  const classes = useStyles();

  const theme = useTheme();
  const desktop = useMediaQuery(theme.breakpoints.up("lg"));
  const tablet = useMediaQuery(theme.breakpoints.up("sm"));
  const mobile = useMediaQuery(theme.breakpoints.up("xs"));

  const resizeQuery = () => {
    console.log("Mahad0");
    if (desktop) setnumberOfShownImages(4);
    else if (tablet) setnumberOfShownImages(3);
    else if (mobile) setnumberOfShownImages(1);
  };

    window.addEventListener('resize',resizeQuery);

  return (
    <div className={classes.paper}>
        <IconButton aria-label="delete" color="primary">
            <ArrowBackIosNewIcon fontSize='large' color='secondary'/>
        </IconButton>
        {
            array.slice(currentIndex, numberOfShownImages)
        }
        <IconButton aria-label="delete" color="primary">
            <ArrowForwardIosIcon fontSize='large' color='secondary'/>
        </IconButton>
    </div>
  )
}

I want to listen for window resize event in react.js. I tried using window.addEventListener but that fires the callback multiple times. Is there a way that the callback runs only once or simply a more react oriented way(a hook maybe) to achieve this?

    const resizeQuery = () => {
    console.log("check");
    if (desktop) setnumberOfShownImages(4);
    else if (tablet) setnumberOfShownImages(3);
    else if (mobile) setnumberOfShownImages(1);
  };

    window.addEventListener('resize',resizeQuery);

full Component code:

function ImageSlider(props) {
  const [numberOfShownImages, setnumberOfShownImages] = useState(props.numberOfShownImages);
  const currentIndex = 0;
  const array = [
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 1"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 2"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 3"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 4"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 5"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 6"/>,
                <PrimaryCard imageHeight = "200" imageAlt = "Fox" imagePath = {fox} header = "Fox" description = "This is a 7"/>,
  ];
  const classes = useStyles();

  const theme = useTheme();
  const desktop = useMediaQuery(theme.breakpoints.up("lg"));
  const tablet = useMediaQuery(theme.breakpoints.up("sm"));
  const mobile = useMediaQuery(theme.breakpoints.up("xs"));

  const resizeQuery = () => {
    console.log("Mahad0");
    if (desktop) setnumberOfShownImages(4);
    else if (tablet) setnumberOfShownImages(3);
    else if (mobile) setnumberOfShownImages(1);
  };

    window.addEventListener('resize',resizeQuery);

  return (
    <div className={classes.paper}>
        <IconButton aria-label="delete" color="primary">
            <ArrowBackIosNewIcon fontSize='large' color='secondary'/>
        </IconButton>
        {
            array.slice(currentIndex, numberOfShownImages)
        }
        <IconButton aria-label="delete" color="primary">
            <ArrowForwardIosIcon fontSize='large' color='secondary'/>
        </IconButton>
    </div>
  )
}
Share Improve this question edited Mar 13, 2022 at 14:47 k200338 Mahad Hameed asked Mar 13, 2022 at 14:38 k200338 Mahad Hameedk200338 Mahad Hameed 471 gold badge3 silver badges6 bronze badges 1
  • Can you post your code for the entire ponent? – Gnanavel Commented Mar 13, 2022 at 14:43
Add a ment  | 

1 Answer 1

Reset to default 2

use windowsize hook

import { useState, useEffect } from "react";
// Usage
function App() {
  const size = useWindowSize();
  return (
    <div>
      {size.width}px / {size.height}px
    </div>
  );
}
// Hook
function useWindowSize() {
  // Initialize state with undefined width/height so server and client renders match
  // Learn more here: https://joshweau./react/the-perils-of-rehydration/
  const [windowSize, setWindowSize] = useState({
    width: undefined,
    height: undefined,
  });
  useEffect(() => {
    // Handler to call on window resize
    function handleResize() {
      // Set window width/height to state
      setWindowSize({
        width: window.innerWidth,
        height: window.innerHeight,
      });
    }
    // Add event listener
    window.addEventListener("resize", handleResize);
    // Call handler right away so state gets updated with initial window size
    handleResize();
    // Remove event listener on cleanup
    return () => window.removeEventListener("resize", handleResize);
  }, []); // Empty array ensures that effect is only run on mount
  return windowSize;
}
Post a comment

comment list (0)

  1. No comments so far