Display a loading spinner upon clicking a button

I'm looking to implement a loader that appears when a button is clicked (for a signup process) without redirecting to another page. How can I achieve this?

Here is my code and what I have attempted so far (but the loader does not appear)

class Signup extends Component {
  constructor(props) {
    super(props);
    this.state = {
     //code related to signup
      isLoading: false
    };
  }

  showLoader = () =>{
    this.setState({ isLoading: true})
    this.signUp()
  }
  
  signUp() {
    //code related to signup

  }

  render() {
    return (
      <View style={style.container}>
        //code related to signup
          <View style={style.footer}>
            <TouchableOpacity
              style={[style.button, style.buttonOK]}
              onPress={() => this.showLoader()}
            >
              <Text style={[style.buttonText]}>Signup</Text>
            </TouchableOpacity>
            <ActivityIndicator animating={this.state.isLoading} size="large" color="#56cbbe" />
          </View>
        </KeyboardAwareScrollView>
      </View>
      </View>
    );
  }
}

Answer №1

To start off, there seems to be an unnecessary closing View tag in your code. If that was just a mistake in the question, you can simplify it using the JS ternary operator. Here's how:

<View style={style.container}>
        //code about signup
          <View style={style.footer}>
            {(!this.state.isLoading) ? <TouchableOpacity
              style={[style.button, style.buttonOK]}
              onPress={() => this.showLoader()}
            >
              <Text style={[style.buttonTesto]}>Signup</Text>
            </TouchableOpacity> : <ActivityIndicator animating={this.state.isLoading} size="large" color="#56cbbe" />}
          </View>
        </KeyboardAwareScrollView>
</View>

For more information on ternary operators, check out here

This code will replace your button with a loading animation.

If you have any more questions, feel free to ask! I'm here to assist!

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

Deploying and operating passport express node on azure: A comprehensive guide

I am currently developing an express node service for Single Sign-On (SSO) using the passport OAuth2 strategy. Here is the structure of my express node application code: AUTH - certs server.cert server.key -index.html -index.js (creates app as expr ...

Utilize AngularJS to Access Global JavaScript Variables

I recently encountered a situation where I needed to incorporate some dynamic functionality into a website I was building. This led me to AngularJS, which I decided to integrate into certain parts of the site rather than the entire thing. Within my JavaSc ...

Link a YAML file with interfaces in JavaScript

I'm currently learning JavaScript and need to convert a YAML file to an Interface in JavaScript. Here is an example of the YAML file: - provider_name: SEA-AD consortiumn_name: SEA-AD defaults: thumbnail Donors: - id: "https://portal.brain ...

Child element casting shadow over parent element

I am currently using box shadow for both the parent (.map) and child (.toggle-button): .map { position: fixed; top: 20px; right: 0; width: 280px; height: 280px; z-index: 9999; box-shadow: 0px 1px 6px 0px rgba(0,0,0,0.3); } .map ...

Exploring jsTree: A Guide to Extracting all Leaf Nodes

Is there a way to extract all leaf nodes (ID & text) from jsTree without using the checkbox UI? Root -----A -----A1 -----A1.1 -----A2 -----A2.1 -----B -----B2 ...

There appears to be a issue with BINDING functionality within Angular

When using two select fields, selecting an item in the first should automatically fill the second with the corresponding subarray. While this functionality works, adding a new field results in all other selections changing as well. How can I prevent this f ...

Javascript Calculator with Dual Input Fields

I have been given a task to create a calculator by tomorrow using Javascript. Unfortunately, I am currently taking a distance course and Javascript has just been introduced in this assignment. While I am familiar with HTML and CSS, Javascript is something ...

When using ODataConventionModelBuilder in Breeze js, the AutoGeneratedKeyType will consistently be set to 'none'

I am working with a straightforward entityframework poco object public partial class Location: Entity { [Key] public int Id { get; set; } public string Description { get; set; } } The baseClass Entity is structured as below public abstract c ...

Can SVN hooks be incorporated into NPM in a way that is comparable to git hooks?

I want to incorporate an npm script that performs linting and testing before executing an svn commit. If there are any failures during the linting or tests, I want the commit process to halt, similar to a git commit hook. Does anyone have any recommendat ...

I am looking to efficiently store various pieces of data in a database by utilizing a singular variable through JS, PHP, and AJAX for streamlined processing and management

I am not very familiar with the technical jargon in programming, so please bear with me if my question is a bit unclear. To provide more clarity, I have shared the code that I have already written. I will elaborate on the issue after presenting the code: ...

Exploring JSON Data with the Wikipedia API

My current project involves extracting text from Wikipedia articles using their API, which is not the most user-friendly tool. I am facing challenges with parsing the JSON object that I receive in return. The key containing the desired text is labeled &apo ...

React Application Issue 'Error: React is not defined'

I've been working on developing an app using react, but for some reason, it's not functioning properly and I'm struggling to pinpoint the issue. The code compiles without errors using babelify, however, it throws an exception during executio ...

Table sorting feature with checkboxes has disappeared from view

This segment of code includes HTML for a table along with JavaScript functionality. When a checkbox is clicked, another table will be displayed: <form class="filter"> <input type="checkbox" id="checkboxID" class="unchecked"> EG <input type= ...

avoiding less than or greater than symbols in JavaScript

I'm encountering an issue while attempting to escape certain code. Essentially, I need to escape "<" and ">" but have them display as "<" and "> in my #output div. At the moment, they show up as "&lt;" and "&gt;" on the page. This ...

Tips for dynamically updating the URL based on a query string within the URL

Here is a table generated by GridView: <table id="ctl00_centerContent_GridView1"> <tr> <th scope="col"> <!-- COLUMN INFORMATION --> </th> </tr> <tr> ...

Issue with PassportJs not forwarding users after successful authentication

I'm facing some challenges with implementing Passport for authentication. I have set up my signup strategy in the following way: passport.use('local_signup', new localStrategy({ usernameField: 'username', passwordField:&apo ...

Using the .splice() method in Javascript to eliminate elements from an array

I am currently working on a function that loops through an array and eliminates any elements that are false, null, 0, undefined, NaN, or an empty string. Instead of returning an empty array as expected, the current output is [null,0,null,null,""]. Below i ...

How can I reset an ng-model value of a range input in AngularJS?

Utilizing a filter with two inputs that share the same ng-model concurrently: <!-- Number input --> <input type="number" maxlength="5" step="0.25" min="-10" max="10" placeholder="0.00" ng-model="sphere"> <!-- Range Input --> <input ...

Combining arrays within an array with the help of lodash

What is the best way to combine arrays within an array using lodash? Consider this scenario: Input: var x = [ [1,2,3,4], [5,6,7], [], [8,9], [] ]; Desired output: x = [1,2,3,4,5,6,7,8,9]; Presently, my code performs the following function: return pr ...

Creating unique geometric designs with three.js

Attempting to construct a polygon in three.js and here is the code used for it. function DeployZone(coordinatesList) { // Forming the polygon Shape { var material = new THREE.MeshLambertMaterial({ color: 0x00ffcc }); var faces = [0, 1, 2, 3, 4] ...