Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How to disable button only if the request is successful - otherwise keep button enabled in react

Im trying to disable a button only if the api request is successful, otherwise if it is not successful, the button should be enabled still. I have the following state field.

this.state = {
  buttonDisabled: false,
};

Here is my button:

<Button type="button" disabled={this.state.buttonDisabled} onClick={this.send} variant="outlined" color="primary">
          Send
 </Button>

However, for some reason, the button is disabled for 10 seconds in both cases. Passed and failed response from the api. Is there something off here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your sendSms() function returns an axios Response object. Therefore, the data object will always be populated and an error is never thrown.

sendSmsCode = async () => {
         const { actions } = this.props;
    
         sendSms(phone)
           .then((data) => {
             // the data object is always populated with the response object and never throws an error
             // therefore, this function will always set the state to disabled
             this.setState({
               requestSmsDisabled: true
             }, () => {
               actions.showSmsNotification(data);
             });
           })
           // this is never invoked
           .catch(err => actions.showSmsNotification(err));
    
         setTimeout(() => {
           this.setState({
             requestSmsDisabled: false
           });
         }, 10000);
       };

You need to check for an error in the response from your axios call, either in the sendSms() function or the sendSmsCode() function, something like:

async function sendSms(phone) {
  const options = {
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    data: {
      phone
    },
    url: `${API_ROOT}/sms`
  };
  // there are a number of ways to do this, it all depends on how you want to do it
  const response = await axios(options);
  if (response.status === 200) {
    return response.data;
  } else {
    // do something to indicate an error, e.g. throw an error to get caught in your .catch() statement or return an error message
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!