0

I am learning React and currently having difficulties understanding how useEffect really supposed to work. What I am trying to accomplish is to make useEffect execute when the object it 'listens' to changes, but it doesn't happen.

I.e. according to the following tutorial https://www.tutorialspoint.com/using-useeffect-in-react-js-functional-component :

We can pass a second argument to useEffect, if there is any change on the second argument then React will execute this useEffect function call.

So here's my code, in which counter is the second argument/the object useEffect "listens" to:


function CounterUseRef() {

  const counter = useRef(0);

  function handleClick() {
    counter.current = counter.current + 1;
    console.log(counter.current);
  }

  useEffect(() => {
    console.log("useEffect"); 
  }, 
  [counter.current]
  //[counter]
  
  );

  return (
    <div>
      <h1>Counter {counter.current}</h1>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
export default CounterUseRef;

When the button is clicked, handleClick() gets executed and counter.current gets incremented, so I thought this would constitute "any change on the second argument". However, setEffect doesn't get executed. It doesn't get executed even if I make counter.current itself the second argument.

Would really appreciate any help.

P.S. I do realize for a use case like this, useState should have been used instead. What I really trying to figure out why useEffect is not working.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Lumachado
  • 1
  • 1
  • 1
    I don't think you're supposed to put a ref in a useEffect dependency array. Isn't the point of a ref is that it doesn't affect renders? https://stackoverflow.com/questions/60476155/is-it-safe-to-use-ref-current-as-useeffects-dependency-when-ref-points-to-a-dom – evolutionxbox Mar 16 '21 at 20:28
  • 1
    @evolutionxbox I wouldn't agree with that 100%, as sometimes you want to run an effect once the ref exists, so you'll put it in the dependency array and wrap the logic inside the effect in a `if (ref.current)`. But in *this* case, a ref is not the way to go. `counter` is better off as a state variable – Seth Lutske Mar 16 '21 at 20:31
  • 1
    I see the question has been closed, but the short answer to your question is: the function doesn't run again because your component never rerenders - as you can also observe from the fact that the counter value in the

    always displays 0. Changing a ref doesn't cause a rerender, only updating props or state does that. And effects can only run after a render.

    – Robin Zigmond Mar 16 '21 at 20:32
  • 1
    @SethLutske but in that scenario I don't think it's the ref that causes the useEffect to run? – evolutionxbox Mar 16 '21 at 20:32
  • @RobinZigmond I'm not sure who marked this as a dupe.... I only linked the question because I thought it was related. Maybe it could do with being reopened? – evolutionxbox Mar 16 '21 at 20:33
  • I wasn't criticising the marking as a duplicate - most likely this has been asked before, and I never checked the link. I was simply trying to give the OP a simple explanation of what they were (or rather weren't) seeing in their component. – Robin Zigmond Mar 16 '21 at 20:34
  • (@RobinZigmond I didn't think you did ) – evolutionxbox Mar 16 '21 at 20:35
  • 1
    @evolutionxbox "I don't think it's the ref that causes the useEffect to run" you are making me rethink my life – Seth Lutske Mar 16 '21 at 22:52
  • Thank you, guys, for clarifying things for me. Really appreciate your help – Lumachado Mar 18 '21 at 20:22

0 Answers0