79780855

Date: 2025-10-02 11:22:58
Score: 0.5
Natty:
Report link

Isn't this whole thing being made needlessly complex?

What is the correct way to provide that callback with the latest state?

The trick is to store the variable outside React and take the value from that, perform whatever needed on that value, and then update the React variable for triggering in useEffect() etc. https://playcode.io/2567999

import React, {useEffect} from 'react';

// Maintain a copy of the variable outside React
var _count = 0

export function App(title) {
  const [count, setCount] = React.useState(0);

  useEffect(() => {
    intervalTimer = setInterval(() => {

      console.log(`React count=${count}`)
      console.log(`Non React count=${_count}`)

      // Latest value is always available in _count outside React
      // Perform whatever needed on that value
      _count += 1

      // Store in the React variable for rerender / useEffect retrigger etc
      setCount(_count);
    }, 3000);
  }, []);

  return (
    <div>
      Active count {count} <br />
    </div>
  );
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Isn't this who
  • Low reputation (0.5):
Posted by: zehawk