I'm trying to update state object in a functional component through TextInput fields without changing the other entry.
const [state, setState] = React.useState({
foo: 'bar',
baz: 'qux',
});
The TextInputs:
<TextInput
id="foo"
value={state.foo}
onChangeText={(text) => updateState('foo', text)}
/>
<TextInput
id="baz"
value={state.baz}
onChangeText={(text) => updateState('foo', text)}
/>
The (non-working) function to update the state since state hooks don't allow merging states:
const updateState = (key, value) => {
var newState = state;
newState[key] = value;
setState(newState);
};
Here's a prepared snack to play with the code and see it in action.
If you log the state to the console via the button you can see that the state actually updates in the background, albeit just with a single character unless you paste something into the input field. If I run this on Android I can actually see the character(s) for a short moment before the text in the TextInput gets reset.
I suspect this has to do with asynchronous calls? I also tried this answer that uses onChange
instead of onChangeText
but was unable to get it to work.