softare development

Disable Button on Empty Input in React JS

In a React application, you can achieve the same functionality of disabling a button when an input is empty using React state and event handling.

1. Create a New React Application

The first thing we have to do is to create our React application using the command:

npx create-react-app folder-name

2. Add Bootstrap Styling

We’re also going to add a little Bootstrap for ease of design. We will add the following code to our index.html file in the public folder of our application.

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">

Next, we will modify our App.js file as follows:

import React, { Component } from 'react';

class App extends Component{
  render(){
    return(
  <div className="container">
  <form>
  <div class="mb-3 col-md-6">
  <label for="exampleFormControlInput1" class="form-label">Email address</label>
  <input onChange={(e) => this.getValue(e)} type="email" class="form-control mb-4" placeholder="name@example.com" />
  <button type="button" class="form-control btn btn-primary btn-lg">Submit</button>
</div>
</form>
      </div>
    )
  }
}

export default App;

3. Set Initial State of Application

We need to set initial state for our application. What we want is that as the application loads initially, let the button be disabled and then we can conditionally check for input.

We will set the state as follows:

constructor(props){
    super(props);
    this.state = {
      isDisabled: false,
      email: ''
    }
  }

Next, we need to set that state in our button element.

render(){
    const { isDisabled } = this.state;
    return(
  <div className="container">
  <form>
  <div class="mb-3 col-md-6">
  <label for="exampleFormControlInput1" class="form-label">Email address</label>
  <input type="email" class="form-control mb-4" placeholder="name@example.com" />
  <button disabled={isDisabled} type="button" class="form-control btn btn-primary btn-lg">Submit</button>
</div>
</form>
      </div>
    )
  }

4. Create a Function that Gets the Entered Value

Finally, we’re going to create a function that gets the entered value and updates the state.

  getValue = (e) => {
    if(e.target.value === ""){
      this.setState({ isDisabled: true })
    }else {
      this.setState({ isDisabled: false })
    }
  }

Full Code Here

import React, { Component } from 'react';

class App extends Component{

  constructor(props){
    super(props);
    this.state = {
      isDisabled: true,
      email: ''
    }
  }

  getValue = (e) => {
    if(e.target.value === ""){
      this.setState({ isDisabled: true })
    }else {
      this.setState({ isDisabled: false })
    }
  }

  render(){
    const { isDisabled } = this.state;
    return(
  <div className="container">
  <form>
  <div class="mb-3 col-md-6">
  <label for="exampleFormControlInput1" class="form-label">Email address</label>
  <input onChange={(e) => this.getValue(e)} type="email" class="form-control mb-4" placeholder="name@example.com" />
  <button disabled={isDisabled} type="button" class="form-control btn btn-primary btn-lg">Submit</button>
</div>
</form>
      </div>
    )
  }
}

export default App;

Result

Buy Mobile App Templates

Author

Recent Posts

How to Get the Day of a Given Date in JavaScript

When working with JavaScript as a software engineer, date manipulation is a common task. One…

2 hours ago

This Wi-Fi-enabled pizza oven can cook pizzas in just two minutes.

Introducing the Model P Smart WiFi-enabled Pizza Oven, the latest innovation in pizza-making that lets…

1 day ago

npx create-react-app is deprecated

If you’ve been developing with React for a while, chances are you’ve used npx create-react-app…

1 day ago

How to Get Started With Amazon Web Services (AWS)

Amazon Web Services (AWS) is one of the leading cloud platforms in the world, offering…

5 days ago

Apple TV+ Announces Free Streaming Weekend This January

Apple is set to make its streaming service, Apple TV+, free for all users during…

5 days ago

Hackers Inject Malicious Code into Chrome Extensions in Bold New Attack

This month, hackers successfully modified several Chrome extensions with malicious code after infiltrating admin accounts…

1 week ago