【发布时间】:2021-05-15 21:15:44
【问题描述】:
所以我正在尝试使用一个名为 react-alert 的库进行错误处理。
我使用 Axios 作为我的 REST 处理程序。我创建了一个动作和一个减速器,它们都工作正常;当我发出发布请求时,在我的 DevTools 扩展中看到了我的状态更新。
这是我的名为 Alert.js 的功能组件
import React, { Fragment, useState } from "react";
import { useAlert } from "react-alert";
import { useSelector } from "react-redux";
import { useEffect } from "react";
export default function Alert(props) {
const alert = useAlert();
const error = useSelector((state) => state.errors.errors);
const [errorMSG, setERRORS] = useState();
useEffect(() => {
if (error !== errorMSG) {
setERRORS(error);
alert.show(errorMSG);
} else {
alert.show("Welcome!");
}
}, [errorMSG]);
return <Fragment />;
然后我在我的主 App.js 中调用它
//Main Imports
import React, { Component, Fragment } from "react";
import ReactDOM from "react-dom";
// React Alert
import { Provider as AlertProvider } from "react-alert";
import AlertTemplate from "react-alert-template-basic";
import Alert from "./layout/Alert";
const alertOptions = {
timeout: 3000,
position: "top center",
};
//Components import
import Header from "./layout/Header";
import Dashboard from "./products/Dashboard";
//Redux import
import { Provider } from "react-redux";
import store from "../store";
class App extends Component {
render() {
return (
<Provider store={store}>
<AlertProvider template={AlertTemplate} {...alertOptions}>
<Alert />
<Fragment>
<Header />
<div className="container-fluid">
<Dashboard />
</div>
</Fragment>
</AlertProvider>
</Provider>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
我确实看到了欢迎!当应用程序安装时,但当我有错误时,我什么也看不到。任何人都可以帮我找到解决方案吗?将不胜感激。
【问题讨论】:
-
组件的本地状态出现错误有什么意义?
-
什么都没有。只是我尝试了不同的事情,因为我找不到解决方案,但现在我正在使用 Redux 存储状态,它工作得很好!
标签: javascript reactjs redux react-redux axios