Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda