React Native outperforms in rendering speed compared to its loading capabilities from Firebase

I am currently developing an application where users can choose stores and save them to their profile. The user authentication is managed through Google Firebase, which handles the login process. When a user accesses the profile screen, the data is retrieved from Firebase and displays the name along with the selected stores.

I have a flatlist of all the stores that are loaded locally (hardcoded), containing a list of shops that users can select or deselect. If a store is selected, the background color changes to indicate its selection. When loading from Firebase, any previously selected stores should also reflect this change in background color.

  const selectStore = (index) => {
    const data = [...stores];
    data[index].selected = !data[index].selected;
    store(data);
  };

Upon revisiting the screen, the data is fetched from Firebase and displayed to the users. However, there is an issue where the screen loads faster than it can render all the Firebase data. As a result, only the list of stores is displayed initially, and the selections become visible only after refreshing the screen. I aim to ensure that sufficient time is given for all data to be loaded before setting the page to true.

To retrieve the current user's logged-in information, I utilize the following code snippet:

useEffect(() => {
    setLoading(true);
    var user = firebase.auth().currentUser;
    if (user) {
      fetchUser(user.uid);
    } else {
      alert("If you see this message, something has gone wrong!");
    }
  }, []);

In the Firebase collection, various user data is stored, including the selected stores as an array:

  const fetchUser = async (userId) => {
    const userRef = db.collection("users").doc(userId);
    const doc = await userRef.get();
    if (!doc.exists) {
      console.log("No such user!");
    } else {
      if (doc.data().fname.length === 0) {
        setUseFirstName("");
        setUseLastName("");
        setShopList([]);
      } else {
        setUseFirstName(doc.data().fname);
        setUseLastName(doc.data().lname);
        setShopList(doc.data().supermarks);
        for (let i = 0; i < shopList.length; i++) {
          let index = shopList[i].id;
          selectStore(index - 1);
        }
      }
      setLoading(false);
    }
  };

When displaying the user information, a loading indicator appears when loading is true. Then, it only shows the first and last names without selecting the stores. How can I address this issue effectively? I hope my question is clear enough.

Answer №1

It's a bit unclear what exactly you're inquiring about, but utilizing the loading flag to display a loading indicator can be achieved like this:

if(loading) {
    return (
      <View>
        <ActivityIndicator animating={true}/>
        <Text>Loading your information...</Text>
      </View>
    )
  } else {
    return (
      [Insert rest of your code here]
    )
  }

Answer №2

In order to display a loading icon while waiting for data from Firebase, it is recommended to utilize the ActivityIndicator component.

For more information on how to implement this, refer to the following link: https://reactnative.dev/docs/activityindicator

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

Utilizing a variety of textures across various surfaces of a single geometry

I'm new to working with Three.js and I have a question about displaying multiple images over a plane geometry. Here is the scenario: Imagine a simplified case where we have a plane divided into tiles like this: +---+---+---+ | 1 | 2 | 3 | +---+- ...

Enforce the splicing of the ng-repeat array with the utilization of track by

Our app incorporates a task list that can potentially grow to a substantial size. The main task list is accompanied by a sidebar, where selected tasks can be edited using a different controller (TasksSidebarCtrl instead of TasksCtrl which handles the list ...

Angular JS is failing to trigger the change event for a checkbox that has already been checked

Be sure to take a look at this link. I am noticing some strange behavior with Angular's on change event when dealing with an initially checked checkbox. Interestingly, the jQuery event seems to work fine in this situation. The angular event only trigg ...

Testing Angular services by injecting dependencies

I'm currently working on test driven development for an angular service using Jasmine. However, I've encountered an issue with resolving the $resource dependency, which is causing a stumbling block at the beginning of my process. The specific er ...

Has the removal of Javascript conditional compilation been confirmed in IE11?

I have been using javascript conditional compilation to determine the version of the IE Trident engine: var ieVersion = undefined; /*@cc_on ieVersion = Math.floor(@_jscript_version); @*/ Despite working fine for IE8, 9, and 10, in IE11, the conditiona ...

What are the methods to transmit various data formats in an AJAX POST request?

I am facing an issue with sending a JSON object along with two file upload objects to the controller in JavaScript. I have tried the following code snippet: data: {"jsonString":jsonString, "fd":"fd", "fd1":"fd1"}, Is there any other way to achieve this, ...

Unable to connect to Server API endpoint

Encountering an issue while trying to access the API endpoint from server.js that connects to Spotify API. Below is my server.js code: // server.js const express = require('express'); const app = express(); const dotenv = require('dotenv&apo ...

Updating the dynamic site script to be compatible with the newest version of the jQuery library

After finding a script on http://css-tricks.com/dynamic-page-replacing-content/, I decided to edit it to suit my needs. Initially, everything worked perfectly with "jquery-1.4.4". However, when I tried using versions later than "jquery-1.5", the active cla ...

Generate a new perspective by incorporating two distinct arrays

I have two arrays containing class information. The first array includes classId and className: classes = [ {classid : 1 , classname:"class1"},{classid : 2 , classname:"class2"},{classid : 3 , classname:"class3"}] The secon ...

What is the best way to extract data from a directory and store it in an array?

import * as fs from 'fs'; const filesDirectory: string = './src/200k'; function findUniqueValues() { let valuesArr : string[] = []; fs.readdir(filesDirectory, function(error, files){ files.forEach(function(file){ fs.read ...

Drawing the canvas is taking place prior to the image being fully loaded in JavaScript

Currently, I am in the process of creating a collage using images that are all sized at 174x174 pixels. The images are stored on my server, and while my program can locate them successfully, it requires me to click the "save image" button twice for them to ...

When adding an object to an array in AngularJS, it seems to trigger updates for

Currently, I am fetching data from a WebAPI and then storing it in an array called products, which is scoped. $scope.products In addition to the products array, I also have another scoped array named selectedFish. $scope.selectedFish = []; My objective ...

Why is access to fetch from the origin localhost blocked by CORS policy, while posting using ajax does not face this issue?

Let's dive into the difference between AJAX and Fetch without involving CORS. I want to understand why an AJAX POST request works flawlessly while a Fetch POST request fails! Currently, I am using jQuery and AJAX for both GET and POST operations. Whe ...

Guide on injecting javascript code containing php variables into a php page

I have successfully developed a PHP page and Javascript code that includes some PHP variables. The code is designed to insert PHP variables into the database using Javascript: <?php $id = "Kevin"; $user = "Calvin"; ?> <!-- include jquer ...

Animating content with Jquery Waypoints for a seamless and elegant user experience

Currently tackling a project that requires some jQuery functions beyond my current skill set. Hoping to find some solutions here! The project in question is a one page scroll site, with existing jquery and waypoints functions implemented. Check out the c ...

Unable to enable text input focus while transitioning to a new screen

I am trying to automatically focus a react-native-paper TextInput when navigating to a screen using react-native-navigation. Despite setting autoFocus={true} on the TextInput, it did not work as expected. Another approach I took was to manually focus the ...

Using React to easily rearrange images by dragging and dropping them

Currently, I am working on incorporating drag-and-drop functionality using React JS along with the react-dropzone library to display thumbnails. The code implementation is provided below: import React from "react"; import ReactDOM from "react-dom"; impor ...

Does the code I've written successfully interact with the AJAX request I'm attempting to make to the Servlet?

I am currently in the process of verifying whether the AJAX Call present in my js file is functioning correctly and sending information back to the Servlet. My goal is to understand how to utilize the response from the servlet to display the user-provided ...

The height appears differently on Mozilla compared to the Chrome browser

I currently have three sections on my website: "Header" with a height of 100px. "Content Area" with a height of 462px. "Footer" with a height of 100px. The strange issue I am facing is that in Chrome, the scroll bar is invisible (which is ideal), but in ...

Javascript leaping across intervals

I'm currently working on creating a unique JavaScript slideshow with a customized pause interval for each slide. Let's say I have a slideshow with 3 slides and I want the pause intervals to be as follows: 30 seconds 8 sec 8 sec |---- ...