Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
Why can't clear error message after show - axios - react

First question for context

I'm showing using with localhost:3000/users/:id API has 10 user so if request to localhost:3000/users/11 should show error message

Also want to show message for connection problems too I've tried to add setError(""); inside finally block but error message stopped working.

If I don't add this time working when I get error but when I fix error error still appear.

I want to show user details again

import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import axios from "axios";

function User() {
  const { id } = useParams();
  const [user, setUser] = useState(null);
  const [error, setError] = useState("");
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    axios("https://jsonplaceholder.typicode.com/users/" + id)
      .then((res) => setUser(res.data))
      .catch((error) => setError(error.message))
      .finally(() => setIsLoading(false));
  }, [id]);
  return (
    <div>
      {error === "Request failed with status code 404" ? (
        <p>{error}</p>
      ) : isLoading ? (
        <h2>Loading...</h2>
      ) : (
        <div>
          <h3>User Info</h3>
          <p>Name: {user.name}</p>
          <p>Email: {user.email}</p>
          <p>Phone: {user.phone}</p>
        </div>
      )}
    </div>
  );
}

export default User;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to clear your error when the response is OK

.then((res) => {
   setUser(res.data);
   setError("")
})

Here is a Sandbox

----

import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import axios from "axios";

function User() {
  const { id } = useParams();
  const [user, setUser] = useState(null);
  const [error, setError] = useState("");
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    axios("https://jsonplaceholder.typicode.com/users/" + id)
      .then((res) => {
        setUser(res.data);
        setError("")
      })
      .catch((error) => setError(error.message))
      .finally(() => setIsLoading(false));
  }, [id]);
  return (
    <div>
      {error === "Request failed with status code 404" ? (
        <p>{error}</p>
      ) : isLoading ? (
        <h2>Loading...</h2>
      ) : (
        <div>
          <h3>User Info</h3>
          <p>Name: {user.name}</p>
          <p>Email: {user.email}</p>
          <p>Phone: {user.phone}</p>
        </div>
      )}
    </div>
  );
}

export default User;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda