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

204
Views
Differentiate between 404 error and is loading in react and render the components base on that. React

I am trying to render a skeleton component on loading of the data or 404 error component if data not found so far i have tried using if else statements and logical operators so far none work properly.

const [post, setPost] = useState(null);
const [postExternal, setPostExternal] = useState([]);

const fetchPost = () => {
  axios.get(`${API_ONE}/posts?id=${id}`).then((response) => {
    setPost(response.data);
  });
  axios.get(`${API_TWO}/posts?id=${id}`).then((response) => {
    setPostExternal(response.data);
  });
  return;
};

const location = useLocation();
const id = location.pathname.split('/')[2];

useEffect(() => {
  fetchPost();
}, [id]);

{
  post && postExternal && (
    <div>
      <h1>{post.title}</h1>
      <img src={postExternal} />
    </div>
  );
}
{
  !post &&
    (post && post.id === postExternal.id ? (
      <NotFound message='not found' />
    ) : (
      <SkeletonItemPage />
    ));
}

Note: The data is fetched from two different apis

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

0

Define a loading state variable as the follow:

const [isLoading, setIsLoading] = useState(false);

Then your fetchPosts function would be like:

  const fetchPost = () => {
            const apiOnePromise = axios.get(`${API_ONE}/posts?id=${id}`);
            const apiTwoPromise = axios.get(`${API_TWO}/posts?id=${id}`);
            //toggle loader
            setLoading(true);
            Promise.all([apiOnePromise, apiTwoPromise])
                .then(values => {
                    //handle your responses here
                })
                .finally(() => {
                    //toggle loader again
                    setLoading(false);
                })
        };
 

Using this loading variable you can differentiate between the statues, so with your jsx you can do:

   { loading && <SkeletonItemPage /> }
   { !loading && posts.length === 0 && <NotFound message='not found' />  }
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!