1.

What is State and how is it used in React Native?

Answer»

It is used to control the components. The VARIABLE data can be stored in the state. It is mutable MEANS a state can change the value at any time.

import React, {Component} from 'react'; import { Text, View } from 'react-native'; export default class App extends Component { state = { myState: 'State of Text Component' }updateState = () =&GT; this.setState({myState: 'The state is updated'})RENDER() {return (<View> <Text onPress={this.updateState}> {this.state.myState} </Text> </View> ); } }

Here we create a Text component with state data. The content of the Text component will be updated whenever we CLICK on it. The state is updated by event onPress .



Discussion

No Comment Found