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, {Component} from 'react';
import { Text, View, Button } 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
Post a Comment