Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ActivityIndicator Component | Advanced Concepts
Foundations of React Native

ActivityIndicator ComponentActivityIndicator Component

Theory

The ActivityIndicator component in is used to indicate that a task is ongoing, providing visual feedback to the user. It typically appears as a spinning wheel or other animated indicator to convey that the application is busy performing an operation.

Why Do We Need It?

Indicates that the application is working on a task, preventing the user from assuming the application has frozen.

How to Work with ActivityIndicator

  • The ActivityIndicator is straightforward to use and does not require state management;
  • We can control its visibility by conditionally rendering it based on the status of a task.

Example

Code Description
  • Lines 11-12: isLoading: Manages the loading state, initialized as false.
    additionalData: Holds additional data fetched, initialized as null.
  • Lines 14-21: Simulates data fetching by setting isLoading to true and then, after a delay of 3 seconds, sets isLoading to false and updates additionalData.
  • Lines 26-28: Triggers data fetching when pressed.
  • Lines 29-35: Conditional Rendering ({isLoading ? ... : ...})
    Renders an ActivityIndicator while loading (isLoading is true).
    Renders additional data (additionalData) when loading is complete (isLoading is false) and additionalData is available.

Explanation

  • Initially, the component renders a welcome message using the Text component and a button using the TouchableOpacity component;
  • When the button is pressed, the fetchData function is called, which sets isLoading to true to display the ActivityIndicator;
  • After a 3-second delay (simulating data fetching), the fetchData function sets isLoading back to false and updates the additionalData state with some text;
  • The ActivityIndicator is displayed while isLoading is true, and once isLoading becomes false, the additionalData is shown using the Text component.

Result

In Practice

Все було зрозуміло?

Секція 4. Розділ 2
course content

Зміст курсу

Foundations of React Native

ActivityIndicator ComponentActivityIndicator Component

Theory

The ActivityIndicator component in is used to indicate that a task is ongoing, providing visual feedback to the user. It typically appears as a spinning wheel or other animated indicator to convey that the application is busy performing an operation.

Why Do We Need It?

Indicates that the application is working on a task, preventing the user from assuming the application has frozen.

How to Work with ActivityIndicator

  • The ActivityIndicator is straightforward to use and does not require state management;
  • We can control its visibility by conditionally rendering it based on the status of a task.

Example

Code Description
  • Lines 11-12: isLoading: Manages the loading state, initialized as false.
    additionalData: Holds additional data fetched, initialized as null.
  • Lines 14-21: Simulates data fetching by setting isLoading to true and then, after a delay of 3 seconds, sets isLoading to false and updates additionalData.
  • Lines 26-28: Triggers data fetching when pressed.
  • Lines 29-35: Conditional Rendering ({isLoading ? ... : ...})
    Renders an ActivityIndicator while loading (isLoading is true).
    Renders additional data (additionalData) when loading is complete (isLoading is false) and additionalData is available.

Explanation

  • Initially, the component renders a welcome message using the Text component and a button using the TouchableOpacity component;
  • When the button is pressed, the fetchData function is called, which sets isLoading to true to display the ActivityIndicator;
  • After a 3-second delay (simulating data fetching), the fetchData function sets isLoading back to false and updates the additionalData state with some text;
  • The ActivityIndicator is displayed while isLoading is true, and once isLoading becomes false, the additionalData is shown using the Text component.

Result

In Practice

Все було зрозуміло?

Секція 4. Розділ 2
some-alt