State in React Native

 1) How to use State in React Native in Class Component

State:-State is very common in react native, the state is mutable property which we can change the value at any time.

import React, {Componentfrom 'react';  
import { TextViewButton } from 'react-native;  
  
export default class StateExample extends Component {  
    state = {  
        myState:0 //this is default state value
    }  
    //here we are updating the state through below function
    updateState = () => this.setState({myState: this.state.myState + 1})  
    render() {  
        return (  
            <View style={{
              flex:1,
              justifyContent:'center',
              alignItems:'center',
            }}>  
                <Text style={{
                  fontSize:50,
                  marginVertical:20
                }}> {this.state.myState} </Text>  
                <Button 
                title="Click Here to Update State"
                onPress={this.updateState} // here we calling function to update state
                />
            </View>  
        );  
    }  
}  

OUTPUT

Before Updating the State


After Updating the State



Comments

Popular posts from this blog

How to set different color for every item in FlatList in React Native

ScrollView in React Native

Button in React-Native