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

The Firebase database reference function is not recognized in Node.js

Could someone help me identify the issue in my code before I revert back to MongoDB? The project is built with Node.js (Next.js) This is how I've configured Firebase (works for Google Login authentication): import { initializeApp } from 'fireba ...

Exploring the values of a JavaScript promise while iterating through a for loop

I find myself wandering in the land of possibilities and would greatly appreciate some direction. After spending 2-3 hours scouring through countless SO questions and documentation related to my current predicament, I still seem to be missing the mark. Ove ...

Error: Node.js encountered an unexpected illegal token

Attempting to use the command cd to switch directories to C:\xampp\htdocs\project. Encountering problems with node.js command prompt. ...

Ways to update a component upon becoming visible in Angular

Within my Angular 4.3.0 project, the header, left panel, and right panels are initially hidden on the login page until a successful login occurs. However, once they become visible, the data is not pre-loaded properly causing errors due to the ngOnInit meth ...

Node.js can be utilized to make multiple API requests simultaneously

I am facing an issue while trying to make multiple external API calls within a for loop. Only one iteration from the for loop is sending back a response. It seems that handling multi-API requests this way is not ideal. Can you suggest a better approach fo ...

Error encountered in creating dependency tree while utilizing create-react-app and storybook

TLDR: How can I configure storybook to use babel-loader v8.1.0 OR force react-scripts to use babel-loader v^8.2.2? Details I am developing a library with an example folder that is a project created with create-react-app. I wanted to integrate storybook i ...

Can you tell if there are any distinctions between the two code snippets?

Initial Code console.log('Begin'); // output 1 await axios({ method: 'post', url: '<HTTP_URL>' data: <SOME_DATA>, }).then ((response) => { // Performing some action... This may take a few seconds. con ...

Ways to determine if the consuming application is utilizing Next.js

Currently, I am in the process of developing a versatile component library that can seamlessly integrate with both vanilla React and Next.js applications. One dilemma I am facing is determining how to identify if a particular component is being utilized w ...

React is experiencing port number conflicts

I encountered a strange issue in my React app. In my server.js file, I specified the port number as: const port = process.argv[3] || 3500; After that, I included the following code at the end of the same file: app.listen(port, () => console.log(`Web ...

Efficient Ways to Reduce Textfield Re-renders

One issue that has caught my attention is the fact that every component within the same parent element (App in the example below) seems to rerender when unrelated states/props change, causing noticeable slowdown in the page/forms. Despite following variou ...

Set up a personal build using NPM version 8 on your local machine

I am transitioning from Node 14 (with NPM 6) to Node 16 (with NPM 8) for my Angular application. This particular application consists of a project that we bundle as an NPM package. During local development, we utilize a script to build this project loca ...

Tips for managing CSRF solely for POST requests without any GET requests

My CMS has a delete post route that is not working due to a cross-site forgery detection issue. Other routes are functioning correctly because there is one pair of GET POST available, but for the delete route, I only have one POST route. The CMS is based o ...

Using create-react-app with TypeScript for server-side rendering

My current project is built with create-react-app using typescript (tsx files). I'm now interested in implementing SSR for the project, but I'm not exactly sure where to begin. In the past, I've successfully implemented SSR with typescript ...

"To ensure consistent alignment, position list items with varying heights at the bottom of the

Is there a way to align a series of li items to the bottom of their parent, the ul, without using any unconventional methods? The challenge lies in the fact that these li items need to have a float: left property and vary in height and width. Below is my ...

Display the closest locations on the Google Maps interface

I am currently working on a project that involves integrating Google Maps. The project includes storing hospital addresses (in longitude and latitude) in a database. However, I need assistance in displaying the nearest hospital from my current location. I ...

RabbitMQ - determining the optimal prefetch count for each consumer

Could someone clarify the usage of the node-amqp library for creating multiple consumers in RabbitMQ? I am unsure if the prefetch-count option applies to each individual consumer or if it is shared among all consumers. I want each consumer to have its own ...

What is the best way to retrieve the parameters and query strings from a Next.js Link within the getServerSideProps function?

I am facing an issue with a component that links to a NextJS page where I need to access both the params and the query string using getServerSideProps. My goal is to have a clean URL structure like 'domain.com/username/likes' without any query p ...

Creating a reference to a hyperlink or button in a variable

I am currently working on creating a login form for a school project website. The section that is posing a challenge for me is determining if I can assign a variable to a href or button. For instance, imagine that the href has the variable 'A' ...

Building a WordPress page with customized fields utilizing Node.js

I have been attempting to create a WordPress page with custom fields. The code snippet below adds a custom field, but not in the correct manner. The field name is 'y' and its value as well. client.newPost({ type: "page", title: "a P ...

Vue.js versatile form for both adding and editing

As a newcomer to the world of vue.js, I am currently working on expanding some tutorials that I have completed. After struggling with this for three hours now, I must admit that I am feeling quite frustrated. Just to give you a heads up, I am using firebas ...