Building a loading spinner component in a react-native project

I have successfully implemented a loading spinner that is currently being used in various components of my React project. However, as I am now working on a react-native version of the application, I am faced with the challenge of recreating this loading spinner in react-native.

Despite my efforts to utilize Animated from react-native, I have been unable to achieve the desired result. My goal is to replicate the exact same animation in the react-native environment.

.container {
  height: 200px;
  width: 200px;
  position: relative
}

.load {
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%);
  width:60px;
  height:60px;
}

.loader {
  border:0;
  margin:0;
  width:40%;
  height:40%;
  position:absolute;
  border-radius:50%;
  animation:spin 2s ease infinite
 }

.load1 {
  background:#19A68C;
  animation-delay:-1.5s
}
 
.load2 {
  background:#F63D3A;
  animation-delay:-1s
}

.load3 {
  background:#FDA543;
  animation-delay:-0.5s
}

.load4 {
  background:#193B48
}

@keyframes spin {
  0%,100%{transform:translate(0)}
  25%{transform:translate(160%)}
  50%{transform:translate(160%, 160%)}
  75%{transform:translate(0, 160%)}
}
<div class="container">
  <div class="load">
    <div class="loader load1"></div>
    <div class="loader load2"></div>
    <div class="loader load3"></div>
    <div class="loader load4"></div>
  </div>
</div>

Answer №1

In my opinion, Reanimated is a great choice for an animation library. However, it's important to note that you may need to rewrite your animations (such as delays and spins) using react-native-reanimated while maintaining your other styles like background or margin.

Answer №2

For displaying a loading indicator in React-Native, you can simply utilize the ActivityIndicator component.

import { ActivityIndicator } from "react-native";

/......

<ActivityIndicator size={50} color={"white"} />

/......

Answer №3

When it comes to React Native, using traditional JavaScript styles is not the way to go if you want to implement a loading spinner. A more effective approach would be to utilize Lottie Animation by fetching JSON data and then rendering it with the help of the lottie-react-native package.

In my case, I created a custom loading spinner from Lottie Animation and turned it into a component like so:

import React, {useMemo} from 'react';
import {View} from 'react-native';
import LottieView from 'lottie-react-native';
import myAnimation from '..//..//assets/animations/loadingSpinner.json';
import colouriseLottie from '../CustomLoadingSpinner';
import styles from './style';
import AppColor from '../../constants/Colors';

const LoadingSpinner = ({containerStyle}) => {
  const {APP_DEFAULT_COLOR} = AppColor();

  const colorizedSource = useMemo(
    () =>
      colouriseLottie(myAnimation, {
        'layers.0.shapes.0.it.0.it.10.c.k': APP_DEFAULT_COLOR,
        'layers.0.shapes.0.it.1.it.1.c.k': '#fff',
      }),
   [],
  );

  return (
    <View style={containerStyle}>
      <View style={[styles.spinnerView]}>
        <LottieView
          speed={0.5}
          style={{width: 90, height: 90}}
          source={colorizedSource}
          autoPlay
          loop
        />
      </View>
     </View>
   );
};

export default LoadingSpinner;

Please disregard the mention of "colouriseLottie" as I have configured my app to dynamically change colors which are reflected in the custom loading spinner.

I hope this solution works for you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for positioning Bootstrap form labels and input fields in alignment

Currently, I am enhancing bootstrap-4 forms that consist of multiple input fields and labels. The form is functional, but I aim to elevate its visual appeal and user-friendliness. Working Snippet <link rel="stylesheet" href="https://stackpath.bootst ...

Checking for Click Events in React Components

Recently, I created a React component named HelpButton with the following structure: import helpLogo from '../Resources/helplogo.svg'; function HelpButton(props) { const [isOpen, setisOpen] = React.useState(false) function toggle() { ...

Is there a way to utilize req.query, req.params, or req.* beyond its original scope without the need to store it in a database?

Looking to streamline my code and apply the DRY pattern, I've been working on creating a helper function for my express http methods. The structure of each method is similar, but the req.params format varies between them. Here's how I attempted t ...

Why is my Bootstrap row exceeding the page width when I resize it?

As a newcomer to Bootstrap, I have been following tutorials to learn more about it. Currently, I am attempting to recreate the layout of Google's homepage. However, I am facing challenges with the grid system's responsiveness. I have managed to ...

Please provide both an image and text in addition to a progress bar by using the form to

I am working on a form that needs to store both images and text data in a database. <form action="processupload.php" method="post" enctype="multipart/form-data" id="UploadForm"> <input name="name" type="text" /> <input name="age" ty ...

Where could one possibly find the destination of the mysterious <circle>?

I want to implement a simple pulsating animation on a circle svg element. I attempted using a transform: scale(..,..) animation, but it seems to be shifting the entire circle within its container instead of just scaling the element itself. This is the cur ...

AngularJS - Calculate multiple currencies

I need to calculate the product of a value and months. For the input value, I am utilizing a Jquery Plugin to apply a currency mask. Unfortunately, the calculations are not functioning properly with this plugin. My goal is to multiply the value, includin ...

Tips for accurately measuring the height of a single table cell

I am facing an issue with a table that I have set up. Here is the code: <table> <tr> <td id='tdleftcontent' style='border:1px solid red;'> <asp:Label ID='lbl' runat="server"></asp:Label> < ...

Aligning a div in relation to the background image

I am seeking guidance on how to center a Div Box and make it responsive relative to the background image. The div box should be contained within the white box, similar to the example shown here: body{ font-family: 'Roboto Mono', monospace; ...

Enhancing a Pie Chart Dynamically with Ajax using Highcharts

I need assistance with updating the data for the pie chart when a list item is clicked. The issue arises when using dynamic values from $cid in data.php. For example, user_student.cid = 1 works correctly, but if I use user_student.cid = $cid, it doesn&apos ...

Node.js AJAX post causing HTML page to not load properly

When using express.js, I want to be able to redirect to an HTML page after clicking on a table element. Currently, the output in the console.log is the HTML source instead of being redirected and interpreted as an HTML page. Here is my click function usin ...

Conceal the DropDown in Next Js when clicked away from

Recently started learning Next.js and encountered an issue with hiding a dropdown when clicking outside the button. The code works perfectly in create-react-app, but when trying to implement it in Next.js, it doesn't seem to work. const LanguageR ...

Flickering of CSS in Nextjs

Currently, I am in the process of developing a React application using Material UI and Next.js. To customize the appearance of the <Autocomplete /> component provided by Material UI, I am overriding some of its styles with my own defined styles like ...

Difficulty activating JavaScript function - unsure of proper implementation

I'm having trouble with calling a function instead of an alert in my code. I tried using FunctionsName(); and removing the alert(''); but it doesn't seem to be working for me :( Could someone please take a look at the following code an ...

AdonisJS Version 4 is reporting an error with the `belongsToMany` relationship method. The issue is related to the `sync()` and `attach()` functions

Encountering an error: invalid input syntax for type uuid: "" The beforeCreate hook event is functioning properly, inserting the column ID, but the error persists. Is there something crucial I am overlooking? Pivot Table: this.create('customer_promo ...

Guide: Previewing uploaded images with HTML and jQuery, including file names

Any constructive criticism and alternative methods for accomplishing this task are welcomed. I am currently working on writing jQuery code that will allow users to preview file(s) without reloading the DOM. To achieve this, I have been using .append() to ...

How can I implement Before() and After() hooks within a hooks.js file for a Selenium and JavaScript project?

Within my Selenium JS / Cucumber framework, I have incorporated Before() & After() functions to handle the setup and tear down of my webdriver. To manage these functions, I decided to organize them in my customSteps.js file alongside other cucumber st ...

Having trouble with Postgres not establishing a connection with Heroku

My website is hosted on Heroku, but I keep encountering the same error message: 2018-05-06T19:28:52.212104+00:00 app[web.1]:AssertionError [ERR_ASSERTION]: false == true 2018-05-06T19:28:52.212106+00:00 app[web.1]:at Object.exports.connect (_tls_wrap.js:1 ...

Display popup just one time (magnific popup)

Attempting to display this popup only once during a user's visit. It seems like I might be overlooking something. <script src="http://code.jquery.com/jquery-1.7.min.js"> <link href="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1 ...

Enhance the styling of elements generated through JavaScript in VueJs with custom CSS

I need help applying CSS to elements that I dynamically created using JavaScript. buttonClicked(event) { console.log(event); let x = event.clientX - event.target.offsetLeft; let y = event.clientY - event.target.offsetTop; let ripples = document.cre ...