0
let [amount, setAmount] = React.useState(100);

function ChangeHandler(event) {
  let value = event.target.value;
  setAmount(value);
  props.update(amount);
}

props.update is a function that I passed from a higher component to update another Hook.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Amr Khaled
  • 70
  • 2
  • 5
  • 1
    Can you add more details? – Naren Nov 02 '20 at 12:46
  • The update is asynchronous, more in the linked question's answers and [the documentation](https://reactjs.org/docs/faq-state.html#why-is-setstate-giving-me-the-wrong-value). `setAmount` **will** definitely update your state item. It won't update the in-scope variable that functions may close over; you need to make sure your functions don't have state closures. (Note: use `const` when declaring your state members: `const [amount, setAmount] = React.useState(100);`. It helps you remember that *that* variable will never change, the change is in the one you get **next time** you call `useState`.) – T.J. Crowder Nov 02 '20 at 12:49

1 Answers1

1

You can't access the state after setting it in next line.

do it like that

let [amount, setAmount] = React.useState(100);

function ChangeHandler(event) {
  let value = event.target.value;
  setAmount(value);
  props.update(value);
}
Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73