0

I have a react component and want a specific part of its state to update to true when a condition is truthy:

I am trying to use useEffect() to achieve this.

 const [isPatched, setIsPatched] = useState(false);

  useEffect(() => {
    x.log ? setPatched(true) : setPatched(false);
  }, [x.log]);

  const setPatched = (patched: boolean) => {
    console.log(patched); //logs out true
    setIsPatched(patched);
    console.log(isPatched); //logs out false
  };

I can't figure out why isPatched is remaining false despite the function setting it to true.

deadant88
  • 920
  • 9
  • 24
  • 1
    There are already a lot of these answered on stack overflow, but `setIsPatched` is not synchronous, the new state value will be set before react starts to rerender the component. – Jacob Smit Jan 31 '23 at 00:35
  • okay so do i need to use a useEffect to fix this? – deadant88 Jan 31 '23 at 00:42

0 Answers0