Reputation: 794
I am trying to use setState while mapping array in my react native app. i have an array and i want to set array value in my variable current_cat_id. my code is :-
this.state = {
current_cat_id:'',
}
render() {
return(
{this.props.data.map((dataImage,Index)=>
<View key={Index}>
{dataImage['main-head'] != undefined && (
<View style={{marginRight:20,marginLeft:20,marginBottom:20,width:320,fontSize:16,color:'#000'}}>
{dataImage['main-head'].map((subsubchild, Index3)=>
<Text>{subsubchild['cat_name']} ({subsubchild['num_rows']})
</Text>
)}
</View>
)}
</View>
)}
)
}
how can i set {subsubchild['cat_name']} to my current_cat_id. I have tried it like :
{this.props.data.map((dataImage,Index)=>
<View key={Index}>
{dataImage['main-head'] != undefined && (
<View style={{marginRight:20,marginLeft:20,marginBottom:20,width:320,fontSize:16,color:'#000'}}>
{dataImage['main-head'].map((subsubchild, Index3)=>
this.setState:({current_cat_id:subsubchild['cat_name']})
<Text>{subsubchild['cat_name']} ({subsubchild['num_rows']})
</Text>
)}
</View>
)}
</View>
)}
but its return an error.
Upvotes: 0
Views: 34
Reputation: 190
you should try this
componentWillMount() {
var ids = [];
this.props.data.map((dataImage) => {
dataImage['main-head'] != undefined && dataImage['main-head'].map((subchild) => {
ids.push(subchild['path'])
})
})
this.setState({current_cat_id: ids})
}
Upvotes: 1