What is state in React Native?

WBOY
Release: 2023-09-19 13:45:04
forward
593 people have browsed it

Status is the source of data. We should always try to keep our state as simple as possible and minimize the number of stateful components. For example, if we have 10 components that require state data, we should create a container component to hold the state of all these components.

Example 1

When the user presses the button, the button title changes to ON/OFF.

The state is initialized inside the constructor as shown below -

constructor(props) { super(props); this.state = { isToggle: true }; }
Copy after login

isToggle is the boolean value assigned to the state. The button's title is determined based on the isToggle property. If the value is true, the button's title is ON, otherwise it is OFF.

When the button is pressed, the onpress method will be called, which will call setState that updates the isToggle value, as shown below-

onPress={() => { this.setState({ isToggle: !this.state.isToggle }); }}
Copy after login

When the user clicks the button, the onPress event will be called, And setState will change the state of the isToggle property.

App.js

import React, { Component } from "react"; import { Text, View, Button, Alert } from 'react-native'; class App extends Component { constructor(props) { super(props); this.state = { isToggle: true }; } render(props) { return (  
Copy after login

Output

The button will toggle when the user presses it.

React Native 中的状态是什么?

Example 2

Change the text when the user clicks on it.

In the following example, the state is displayed inside the constructor as follows-

constructor(props) { super(props); this.state = { myState: 'Welcome to Tutorialspoint' }; }
Copy after login

The state myState is displayed inside the Text component as follows-

{this.state.myState} 
Copy after login

When the user touches Or when the text is pressed, the onPress event is triggered and the this.changeState method is called, which changes the text by updating the state myState as shown below -

changeState = () => this.setState({myState: 'Hello World'})
Copy after login

import React, { Component } from "react"; import { Text, View, Button, Alert } from 'react-native'; class App extends Component { constructor(props) { super(props); this.state = { myState: 'Welcome to Tutorialspoint' }; } changeState = () => this.setState({myState: 'Hello World'}) render(props) { return (
         
          
           
            {this.state.myState}
           
          
         ); } } export default App;
Copy after login

output

React Native 中的状态是什么?

The above is the detailed content of What is state in React Native?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!