Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS useEffect not rendering after dependency changed

So I'm currently working with React Hooks, and I'm trying to use useEffect. It supposed whenever that dependencies changed, the useEffect would re-render right? But it doesn't work for me. Here's my code :

const [slidesPerView, setSlidesPerView] = React.useState(0)

  React.useEffect(() => {
    setSlidesPerView(() => (window.innerWidth <= 375 ? 1 : 2))
    console.log("rerender?", slidesPerView)
  }, [window.innerWidth])

Everytime I changed the screen size, useEffect won't re-render. I wonder what did I do wrong?

like image 640
panji gemilang Avatar asked Sep 01 '19 02:09

panji gemilang


People also ask

How do I fix React hook useEffect has missing dependencies?

Conclusion # The warning "React Hook useEffect has a missing dependency" occurs when the useEffect hook makes use of a variable or function that we haven't included in its dependencies array. To solve the error, disable the rule for a line or move the variable inside the useEffect hook.

Does useEffect run after state change?

Use the useEffect hook to listen for state changes in React. You can add the state variables you want to track to the hook's dependencies array and the logic in your useEffect hook will run every time the state variables change.

Can we use useEffect without dependency?

It runs when the component is mounted and when it is re-rendered while a dependency of the useEffect has changed. This is powerful, but it is easy to omit dependencies and create bugs in your app. That's why the React team recommends that you include all the variables you use in the useEffect in the dependency array.

Does useEffect run on every re render?

The useEffect Hook Usages. The callback function we pass to the useEffect hook runs the side effects. React runs it on every render of a component by default.


1 Answers

useEffect will respond to either props changes or state changes.

Every time screen size changes component has no idea, if window.innerWidth is changed or not, because it is not in a state or props.

To get it working you need to store window.innerWidth into state, and attach a event listener to your window, whenever window size changes it will get the window.innerWidth and store it into the state, and as state changes your useEffect will get re-run, and finally your component will get re-render.

const [size, setSize] = React.useState(window.innerWidth)


React.useEffect(() => {
    //Attach event on window which will track window size changes and store the width in state
    window.addEventListener("resize", updateWidth);

    setSlidesPerView(() => (size <= 375 ? 1 : 2));
    console.log("rerender?", slidesPerView);

    //It is important to remove EventListener attached on window.   
    return () => window.removeEventListener("resize", updateWidth);
}, [size])

const updateWidth = () => {
    setSize(window.innerWidth)
}

Demo

like image 76
ravibagul91 Avatar answered Sep 28 '22 14:09

ravibagul91