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

113
Views
Display an error message when API is not working

I have table data where I'm fetching that data through API. I need to display an error message on the user screen like something went wrong message

I tried the below code

const File history = () => {
   const [tableData, setTableData] = useState([]);
   const [isVisible, setIsVisible] = useState(true);
   const [errorMessage, setErrorMessage] = useState('');
   const getFileHistoryData = async() => {
    try {
      const response = await axios.get("api");
      const data = await response.data;
      if (data.length > 0) {
        setTableData(response.data);
      }
    } catch (e) {
      console.log(e.message)
    }
  };
  const renderErrorMessage = () => {
    return(
      <div>
         {errorMessage !- & (
             <h1>{'Something went wrong: + errorMessage}</h1>
         }}
      </div>
    )
}
  return (
      <div>
         {this.renderErrorMessage()}
         <MyTableComponent/>
      </div>  
     );
}

How do I display the error in the frontend render the message since it's a functional component?

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

0

As I pointed out in the comment, you should change the function to store the error state, and change the rendering function to show that.

const File history = () => {
   const [tableData, setTableData] = useState([]);
   const [isVisible, setIsVisible] = useState(true);
   const [errorMessage, setErrorMessage] = useState('');
   const getFileHistoryData = async() => {
    try {
      const response = await axios.get("api");
      const data = await response.data;
      setErrorMessage('');
      if (data.length > 0) {
        setTableData(response.data);
      }
    } catch (e) {
      console.log(e.message)
      setErrorMessage(e.message);
    }
  };
  const renderErrorMessage = () => {
    return(
      <div>
         {errorMessage !== '' && (
             <h1>{'Something went wrong: + errorMessage}</h1>
         }}
      </div>
    )
}
  return (
      <div>
         {renderErrorMessage()}
         <MyTableComponent/>
      </div>  
     );
}
about 4 years ago · Juan Pablo Isaza Report

0

You could modify your code as such :

import React, { Component } from 'react',
import axios from 'axios',
import MyTableComponent from 'YOUR_PATH_TO_THIS_COMPONENT',

class File extends Component {
  state = {
    tableData : [],
    isVisible : true,
    errorMessage : '',
  }
  
  componentDidMount = () => {
    axios.get("api")
      .then(response => {
        const fileDataHistory = Object.values(response.data);
        if (data.length > 0) {
          this.setState({
            tableData:fileDataHistory,
          });
        }
      }
      .catch(error => {
        console.log(error);
        this.setState({
          errorMessage:error,
        });
  };
  
  renderErrorMessage = () => {
    return(
      <div>
         {this.state.errorMessage !== '' &&
             <h1>{'Something went wrong:' + this.state.errorMessage}</h1>
         }
      </div>
    )
  };
  
  render() {
    return (
      <div>
         {this.renderErrorMessage()}
         <MyTableComponent/>
      </div> 
    )
  }
};

about 4 years ago · Juan Pablo Isaza Report

0

You could use a state to store the error message when an error occurs and, use that value to display the error message. Instead of checking if the error message exists in the message rendering function, you could alternatively also use && operator to render the message conditionally.

const File history = () => {
   const [tableData, setTableData] = useState([]);
   const [isVisible, setIsVisible] = useState(true);
   const [errorMessage, setErrorMessage] = useState('');
   const getFileHistoryData = async() => {
    try {
      const response = await axios.get("api");
      const data = await response.data;
      setErrorMessage('');
      if (data.length > 0) {
        setTableData(response.data);
      }
    } catch (e) {
      console.log(e.message)
      setErrorMessage(e.message);
    }
  };
  const renderErrorMessage = () => {
    return(
      <div>
             <h1>{`Something went wrong: ${errorMessage}`}</h1>
      </div>
    )
}
  return (
      <div>
         {errorMessage.length && renderErrorMessage()}
         <MyTableComponent/>
      </div>  
     );
}
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!