What I'm trying to achieve is an Array like this
infos = [{type: 'phone', val: '2222222'}
{type: 'email', val: '[email protected]'}]
Here is the state
constructor(props) {
super(props);
this.state = {
rows: [], //this is used for the add component
contactDetails: [{type: '', val: ''}], // where i will add the values
};
}
So I have a two TextInput
<View>
<TextInput label='Type'/>
<TextInput label='Details'/>
</View>
This View can be dynamically added when a button is clicked and can also be deleted. Here is my code for the add and delete button:
addRow(){
this.state.rows.push(index++)
this.setState({ rows: this.state.rows })
}
deleteRow(key){
this.state.rows.splice(key, 1)
this.setState({ rows: this.state.rows })
}
<TouchableOpacity onPress={ this.addRow.bind(this) } >
<Text style={{textAlign: 'center',}}>Add</Text>
</TouchableOpacity>
...
<TouchableOpacity onPress={ () => this.deleteRow(i) } >
<Image source={require('./../img/delete.png')} style={{width:32, height: 32 }} />
</TouchableOpacity>
How can I get the value of it correctly and add it to my Array?
Thank you.
Update
In my TextInput I tried doing this
<TextInput
onChangeText={(text)=>{
let cd = this.state.contactDetails;
cd[i].val = text ;
this.setState({cd});
}}
value={this.state.contactDetails[i]}
label='Details'
/>
but I'm always having an error undefined is not an object (evaluating 'cd[i].val = text')