0

I have been trying to create and submit a basic React Native form without any library. Currently, for testing purposes, I have included only 3 TextInput(s) only.

enter image description here

As soon as the form is submitted only the values of the last TextInput, password here, is shown updated in the state object (in the console). The first two TextInput fields values remain empty as the initial state.

I tried lots of patterns to pass the data from the input fields using the method onChangeText(), but nothing seems to work for me. No matter how many input fields, only the last input field's state is available in the console. Not sure where things are going wrong. Here is the snack expo link to reciprocate the issue:

https://snack.expo.io/0DP-pi6bn

This might be a silly issue that I am unable to figure out, but any help to resolve the same, will be highly appreciated.

Thanks

program_bumble_bee
  • 439
  • 2
  • 25
  • 57
  • 1
    In your example, you are replacing the state with the last value. useState doesn't do partial updates like class components, you will need to get the prev state and use the spread operator to extract it's values. https://stackoverflow.com/questions/55342406/updating-and-merging-state-object-using-react-usestate-hook – nipuna-g Jan 24 '21 at 16:37
  • 1
    @nipuna777 That was so dumb of me. Thanks for pointing it out. It works perfectly fine now. – program_bumble_bee Jan 24 '21 at 16:49
  • 1
    Easy mistake to make! Happy to help. – nipuna-g Jan 24 '21 at 16:53

0 Answers0