Home > Web Front-end > JS Tutorial > body text

How to show loading indicator in React Native?

PHPz
Release: 2023-08-24 20:45:11
forward
1367 people have browsed it

Use loading indicators when we want to tell the user that a request they are making on the UI will take time. If the user clicks the submit button after filling the form, or clicks the search button to get some data.

ReactNative provides an ActivityIndicator component, which can display loading indicators on the UI in different ways.

The basic ActivityIndicator component is as follows-

<ActivityIndicator animating = {animating} color = &#39;#bc2b78&#39; size = "large"
style = {yourstyle}/>
Copy after login

To use ActivityIndicator, you need to import it as follows -

import { ActivityIndicator} from &#39;react-native&#39;;
Copy after login

Here are some important properties provided by ActivityIndicator.

Sr.NoProps and Instructions
1Animation

Animation for the loading indicator. it takes a boolean value true to show the indicator, false to hide the indicator.

2Color

The color displayed by the loading indicator.

3hidesWhenStopped

Stops when the indicator is not animated. Its value is correct incorrect.

4Size

Size indicator. The values ​​range from small to large.

Example: Display of loading indicator

The loading indicator is implemented using ActivityIndicator, so import -# first ##

import { ActivityIndicator, View, StyleSheet } from &#39;react-native&#39;;
Copy after login

This is the ActivityIndicator component used -

<ActivityIndicator
   animating = {animating}
   color = &#39;#bc2b78&#39;
   size = "large"
style = {styles.activityIndicator}/>
Copy after login

The animation is set to the animation variable, and the default setting is true. Call the closeActivityIndicator method in the componentDidMount() function, which will set the animation state to false after 1 minute.

state = { animating: true }
   closeActivityIndicator = () => setTimeout(() => this.setState({
   animating: false }), 60000)
   componentDidMount = () => this.closeActivityIndicator()
Copy after login

This is the complete code to display the loading indicator -

import React, { Component } from 'react';
import { ActivityIndicator, View, StyleSheet } from &#39;react-native&#39;;
class ActivityIndicatorExample extends Component {
   state = { animating: true }
   closeActivityIndicator = () => setTimeout(() => this.setState({
   animating: false }), 60000)
   componentDidMount = () => this.closeActivityIndicator()
   render() {
      const animating = this.state.animating
      return (
         
            
         
      )
   }
}
export default ActivityIndicatorExample
const styles = StyleSheet.create ({
   container: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
      marginTop: 70
   },
   activityIndicator: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
      height: 80
   }
})
Copy after login
Output

如何在 React Native 中显示加载指示器?

The above is the detailed content of How to show loading indicator 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
Popular Tutorials
More>
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!