GigaPudding
GigaPudding

Reputation: 45

Basic state on react native

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

Answers (1)

Saarett
Saarett

Reputation: 61

  1. Can i declare variable outside class

Yes.

  1. Is declaring variable without type was react-native feature ?

No. This is JavaScript. React-Native is a JavaScript library.

  1. 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

Related Questions