Reputation: 45
I new to react native, can someone explain how to change this state variable (I got no error)
class Header extends Component{
state = {
navDoggo : ['Apartment','Services'],
nav_id : 1
}
render(){
return(
<View style={{width : 60, height:60}} onPress ={
() =>
{return (this.state.navDoggo[0] = 'abcd');}
}>
<Text>{this.state.navDoggo[0]}</Text>
</View>
)
}
}
my Question :
1. Can i declare variable outside class ?
2. Is declaring variable without type was react-native feature ?
3. how to change that state object
Sorry for my lack of knowledge for understanding documentation
Upvotes: 0
Views: 36
Reputation: 61
- Can i declare variable outside class
Yes.
- Is declaring variable without type was react-native feature ?
No. This is JavaScript. React-Native is a JavaScript library.
- how to change that state object
this.setState(previousState => (
{ oldKey: newValue }
))
Pay attention that assigning a different value to an array in your state wouldn't trigger any render as it is the same object in the state.
You must read the documentations, this is the most fundamental thing in React. Perhaps, looking at your two first questions, you should start from JavaScript.
Upvotes: 1