在早期版本中,我们可以使用历史记录(history)返回到先前的路由(previous route)。
我该如何在 react-router-dom 的v6版本中实现这个目标?
history.goBack()
我该如何在 react-router-dom 的v6版本中实现这个目标?
history.goBack()
尝试这种方法
import { useNavigate } from 'react-router-dom';
function YourApp() {
const navigate = useNavigate();
return (
<>
<button onClick={() => navigate(-1)}>go back</button>
</>
);
}
如果(prevPage存在 && prevPageUrl包含您的域名URL){跳转到上一页}
否则 {跳转到主页}
- Wale在V6中,
import { useNavigate } from 'react-router-dom';
function App() {
const navigate = useNavigate();
return (
<>
<button onClick={() => navigate(-2)}>Go 2 pages back</button>
<button onClick={() => navigate(-1)}>Go back</button>
<button onClick={() => navigate(1)}>Go forward</button>
<button onClick={() => navigate(2)}>Go 2 pages forward</button>
</>
);
}
以防万一,如果有人像我一样尝试导航回去或者在无法导航回去的情况下导航到其他地方(例如在新标签页中打开链接),似乎没有任何办法在react-router v6中验证历史记录。 但是看起来你可以访问 window.history.state,它有一个 idx 属性,如果你在历史堆栈的开头,那么该属性为零。
可能还有一些坑点我没有遇到,但这对我有用:
import { useNavigate } from 'react-router-dom';
// ...
const navigate = useNavigate();
// ...
if (window.history.state && window.history.state.idx > 0) {
navigate(-1);
} else {
navigate('/', { replace: true }); // the current entry in the history stack will be replaced with the new one with { replace: true }
}
window.history.state.idx
也不总是定义的。 - Taylor Buckner在旧版本的react-router-dom中存在pop函数。
你可以像这样调用它们:
const history = useHistory();
history.pop()
现在在v6中,您可以使用useNavigate函数。
const navigate = useNavigate();
navigate(-1) // you will go one page back
navigate(-2) // you will go two pages back
history.goBack()
代替 pop()
,因为它不起作用。 - Lalit Fauzdar在react-router Links v6中,还有一种使用delta(数字)的方法:
const BackButton = () => {
return (
<Link to={-1}>
Back
</Link>
);
};
很不幸,在 TypeScript 中存在类型错误,Link 组件不接受数字类型,但它仍然可以正常工作。
<NavLink to={-1}>返回</NavLink>
- brunshte-1 as any
,而不是仅仅写成-1
。 - Amin_mmz如果您想返回或者前往其他页面,可以尝试以下操作:
<button onClick={() => navigate(-1) || navigate('/dashboard')}>
Go back or Dashboard
</button>
我想要补充的是,有时候在导航至(-1)的条件下会导致错误页面的情况发生,比如在进行授权检查期间的重定向。
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate(-1);
import { useLocation, useNavigate } from 'react-router-dom';
const location = useLocation();
const navigate = useNavigate();
const currentPath = location.pathname;
navigate('/login', { state: { from: currentPath } });
在导航到登录页面之前,我们将路由状态设置为具有“from”属性,该属性等于当前路由。
在登录后加载页面时,我们首先检查状态的“from”属性中是否存储了先前的路由,就像具有一个级别的历史堆栈一样。
const state = location.state;
if (state?.from) {
// Redirects back to the previous unauthenticated routes
navigate(state?.from);
else {
navigate('/main-page');
}
import { useEffect } from "react";
import {useNavigate } from "react-router-dom";// react-router-dom v6 version
const SecondComponent = () =>{
const navigate = useNavigate();
useEffect(() => {
navigate('/secondComponent')
},[]);
// note: we must have to to provide path like this one
/*
<Routes>
<Route path="/" element={<FirstComponent/>} />
<Route path="/secondComponent" element={<SecondComponent />} />
</Routes>
*/
return(
<>
<h2 >this is Second page</h2>
</>
)
}
export default SecondComponent;
navigate('/secondComponent')
导航到另一个页面。要返回,您必须使用navigate(-1)
。 - man.in.the.jukebox
useNavigate
的内容,链接为https://github.com/ReactTraining/react-router/blob/dev/docs/advanced-guides/migrating-5-to-6.md#use-navigate-instead-of-history - Zac Anger