首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从反应-误差边界使用useErrorHandler钩子复位后行程的误差边界

从反应-误差边界使用useErrorHandler钩子复位后行程的误差边界
EN

Stack Overflow用户
提问于 2022-04-06 07:15:36
回答 3查看 2.4K关注 0票数 6

在应用程序抛出任何错误时,我使用react error边界包来显示掉落的UI。这个包裹对我很好。我需要了解如何重置应用程序错误状态,如果我转到以前的页面,使用浏览器返回按钮,因为回到以前的页面,也显示掉的UI,而不是原来的组件。无论如何,我们可以呈现原始组件吗?

在下面的代码中,用户将在Page2上抛出错误,因为我将空对象作为支持传递。在这种情况下,它将显示后备屏幕。如果我单击“后退”按钮,仍然会在Page1上显示后备屏幕,这是我不希望看到的。

App.js

代码语言:javascript
复制
const errorHandler = (error) =>{
      console.log(error)
}

<BrowserRouter basename={'/bookingtool/obt/'}>
     <ErrorBoundary FallbackComponent={Fallback} onError={errorHandler}>
          <Routes>
               <Route path="/page1" element={<Page1 PageName="Page1" />} />
               <Route path="/page2" element={<Page2 PageName={{}} /> } />
          </Routes>
    <ErrorBoundary />
</BrowserRouter>

Page1.js

代码语言:javascript
复制
import { useErrorHandler } from 'react-error-boundary';
const Page1 = ({PageName}) =>{
     return(<p>{PageName}</p>)
}

Page2.js

代码语言:javascript
复制
import { useErrorHandler } from 'react-error-boundary';
const Page2 = ({PageName}) =>{
     return(<p>{PageName}</p>)
}

Fallback.js

代码语言:javascript
复制
import React from "react";

const Fallback= (props) => {
    return(<h1>Something went wrong</h1>)
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-15 12:27:09

key提供<ErrorBoundary />。每当key更改时,错误边界就会被重置。

在您的示例中,使用useLocation().pathname作为key意味着每当路径更改时它都会重置:

代码语言:javascript
复制
const location =  useLocation();

// ...
return (
  <ErrorBoundary key={location.pathname}>
    {/* ... */}
  </ErrorBoundary>
)

作为一个单独的建议,我会将错误处理移到布局组件中。这将允许在发生错误时保持导航,这是很好的UX。

基本的这里的例子

票数 11
EN

Stack Overflow用户

发布于 2022-04-14 20:14:34

当您导航到别处时,尝试重置错误状态:

代码语言:javascript
复制
const Fallback= ({ error, resetErrorBoundary} ) => {
    const location = useLocation();
    const errorLocation = useRef(location.pathname);
    useEffect(() => {
        if (location.pathname !== errorLocation.current) {
            resetErrorBoundary();
        }
    },[location.pathname])
    return(<h1>Something went wrong</h1>)
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-15 19:00:49

您可以使用以下方式强制重新呈现错误边界,首先创建一个单独的函数组件来保存错误边界,然后将侦听器添加到历史记录中。

代码语言:javascript
复制
import { createBrowserHistory } from "history";
const history = createBrowserHistory();
//a fallback component
const ErrorFallback = () => {
 return <>
   <h1>Something went wrong.</h1>
   <button onClick={() => {
      history.back();
    }}>go back </button>
  </>
}
 
function RoutesContainer() {
 const [update, setUpdate] = useState(false);
 let navigate = useNavigate();
const historyChange = () => {
if (window.location.pathname !== history.location.pathname) {
  navigate(window.location.pathname, { replace: true });
}
};
useEffect(() => {
 history.listen(historyChange)
}, [historyChange])

 return <ErrorBoundary key={window.location.pathname}  FallbackComponent={ErrorFallback}>
<Routes>
  <Route path="/page1" element={<Page1 PageName="Page1" />} />
  <Route path="/page2" element={<Page2 PageName={{}} />} />
</Routes>
 </ErrorBoundary>
  }

当您从page1返回到page2时,history.location.pathname将具有"page2“值,因为您按后退,此值将与window.location.pathname不匹配,因为window.location.pathname具有"page1”值,在此阶段,我们将导航到window.location.pathname,并将此值用作错误边界component.at中的键--我使用了react路由器-dom V6和react v18。

这里有一个完整的用例演示。

代码语言:javascript
复制
    import React, { useEffect, useState } from 'react';
    import { BrowserRouter, Link, Route, Routes } from 'react-router-dom';
    import { createBrowserHistory } from "history";
    import { useNavigate } from "react-router-dom";
    import { ErrorBoundary } from 'react-error-boundary'

    const history = createBrowserHistory();

    const ErrorFallback = () => {
      return <>
        <h1>Something went wrong.</h1>
        <button onClick={() => {
          history.back();
        }}>go back </button>
      </>
    }
    const Page1 = ({ PageName }) => {
      return (<p>{PageName}
        <Link to={'/page2'} >page 2</Link>
      </p>)
    }

    const Page2 = ({ PageName }) => {
      return (<p>{PageName}</p>)
    }
    function RoutesContainer() {
      const [update, setUpdate] = useState(false);
      let navigate = useNavigate();

      const historyChange = () => {
        if (window.location.pathname !== history.location.pathname) {
          navigate(window.location.pathname, { replace: true });
        }
      };

      useEffect(() => {
        history.listen(historyChange)
      }, [historyChange])

      return <ErrorBoundary key={window.location.pathname}  FallbackComponent={ErrorFallback}>
        <Routes>
          <Route path="/page1" element={<Page1 PageName="Page1" />} />
          <Route path="/page2" element={<Page2 PageName={{}} />} />
        </Routes>
      </ErrorBoundary>
    }


    function App() {
      return (
        <BrowserRouter><RoutesContainer /></BrowserRouter>
      );
    }

    export default App;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71762507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档