I'm trying to make a general onChange handler for multiple TextInput s. However when I access the event, the best I can get is event.nativeEvent which has 3 keys.
eventCount, target, and text
target is only a number. I realize from the docs that 'name' is not a prop of TextInput. Is there a way to pass in a prop to the TextInput so I can get it later in onChange and then set the state based on that?
I have 2 TextInputs like this
<TextInput
name='foo'
keyboardType='numeric'
maxLength={4}
onChange={this.handleChange}
/>
<TextInput
name='bar'
maxLength={4}
onChange={this.handleChange}
/>
Thanks
EDIT: Here is what I tried for putting id on TextInput
<TextInput
id='bar'
name='bar'
maxLength={4}
onChange={this.handleChange}
/>
handleChange(e) {
console.log(e.target.id);
const {name, type, text} = e.nativeEvent;
this.setState({baths: text});
}
You cannot supply or mutate the
props
that have already been defined by the documentation.Therefore you can create custom component for
TextInput
asUsage as you would use normally
Or you can simply do:
and then in ts file
onChangeText={() => this.handelChange("yourStateName")}
handleChange = (yourStatename) => (inputtext) => { setState({ [yourStatename]: inputtext })