1

Is it possible, to use state value immediately, for example:

const [counter, setCounter] = useState(0);

const onChange = (value = 42) => {
  setCounter(value); //value === 42
  props.onChange(counter); // counter === 0
}

Without using new variable, like

const onChange = (value = 42) => {
  const val = value;
  setCounter(val); //val === 42
  props.onChange(val); 
}
J. Doe
  • 502
  • 1
  • 7
  • 16
  • Thinking, the correct way to use changed and actual state variable is to use useEffect hook: `useEffect(()=>{ props.onChange(counter) })` – J. Doe Feb 21 '19 at 15:17
  • `useEffect(()=>{ props.onChange(counter) }, [counter])` is what you need – Shubham Khatri Feb 21 '19 at 15:19
  • not really a duplicate. the question is "is it possible to use state value immediately?" and the linked "duplicate" doesn't directly answer that question. – worc Feb 21 '19 at 18:41

0 Answers0