0

Happy new year all, I am struggling a bit with something which I think should be really trivial. I am trying to update a react state using setState but onPress the string does not update straight away instead it updates when I press the second time. Like I said this I think seems like a little issue but maybe a second eye would prove useful.

Below is my code:

const [selection, setSelection] = useState('');
<TouchableOpacity 
   onPress={() => {
       setSelection(() => 'amount');
       console.log(selection);
   }}>
   <Text>By Amount</Text>
</TouchableOpacity>

I have read in different places about useState being asynchronous and it acts as a queue, meaning the initial value would always return, but I really need the state variable to update to the string 'amount'.

Thank you in advance

youngn
  • 9
  • 1
  • 1
    Does this answer your question? [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – Rafael Tavares Jan 03 '22 at 13:23

0 Answers0