首页 > 解决方案 > 如何在反应 js 中捕获 net::ERR_CONNECTION_TIMED_OUT?

问题描述

我是 React js 的初学者。我在控制台中收到此错误。但我想向用户显示这个错误。这是我的简约代码:

const [error, setError] = useState("");

try {
  Axios.post('http://10.20.2.3:5000/accounts/login', {      
    email,
    password
  }).then(response =>{
    if (response.status == "201") {
      history.push("/admin/dashboard");
    }
  }).catch(error => {
      if (error.response) {
        setError(error.response.data.message);
        setLoading(false);
      }
  }).then(function () {
  });
} catch (e) {
  setError(e.response.data.message);
}


<Alert color="danger" isOpen={ error != ""} fade={true}>
    { error }
</Alert>

我是否应该使用 try catch 来显示此类错误。我怀疑我的 try catch 块不正确。

任何人都可以帮忙吗?

标签: reactjsaxiostry-catchreactstrap

解决方案


推荐阅读