Hooks 版本(React 16.8+):
最简版本。
import { useHistory } from "react-router-dom";
export const Item = () => {
let history = useHistory();
return (
<>
<button onClick={() => history.goBack()}>Back</button>
</>
);
};
useHistory()
被 useNavigate()
替代了。所以使用 useNavigate()
代替 useHistory()
就可以了。import { useNavigate} from "react-router-dom";
export const Item = () => {
let navigate = useNavigate();
return (
<>
<button onClick={() => navigate(-1)}>Back</button>
</>
);
};
了解更多关于useNavigate的内容,请访问:https://reactrouter.com/docs/en/v6/hooks/use-navigate
你可以尝试两种选项,一种是使用push
方法,另一种是使用路由的历史记录中的goBack
方法。通常,如果您直接通过Route
方法路由组件或将历史记录属性传递给子组件并使用它,则将提供历史记录props。
以下是示例代码:
this.props.history.push('/') //this will go to home page
or
this.props.history.goBack() //this will go to previous page
针对您的问题,您可以尝试使用push
方法,并提供您要移动到的确切URL。
如需更多参考,请访问https://reacttraining.com/react-router/web/api/history
你想要的是这个:
假设一个人进入了你网站的某一页,比如:www.yoursite.com/category/books/romeo-and-juliet
在这个页面,你想要显示一个“返回”按钮,它链接到上一级目录,即:www.yoursite.com/category/books/
这就是我们在vBulletin论坛等地方著名的面包屑导航系统。
这里是一个基本解决方案:
Original Answer翻译成"最初的回答"
let url = window.location.href;
let backButtonUrl = "";
if (url.charAt(url.length - 1) === "/") {
backButtonUrl = url.slice(0, url.lastIndexOf("/"));
backButtonUrl = backButtonUrl.slice(0, backButtonUrl.lastIndexOf("/"));
} else {
backButtonUrl = url.slice(0, url.lastIndexOf("/"));
}
{backButtonUrl}
作为返回按钮链接。react-router-dom
库中使用它,则需要像这样设置您的URL变量:
let url = this.props.match.url;
class Restaurant extends Component {
constructor(props) {
super(props);
this.props.history.push('/');
this.props.history.push(this.props.match.url);
}
...
history.push
访问任何页面时,记录你正在访问的页面在状态中Route
组件。在这个组件中:如果 history.action === "POP"
并且 "history.state.lastVisited === <some page with back navigation disabled>"
,那么你应该使用 history.replace
重定向用户到 /home 页面location.reload()
。这将重新加载应用程序,并且历史记录将被重置const navigate = useNavigate();
const location = useLocation();
const goBack = () => {
navigate(-"1);
}
location.state?.canBack // check if user can back
我们想为我们的React应用程序找到类似的解决方案,但不幸的是这是我们想出的最好的解决方案。当我们的用户在移动设备上从广告或referrer
着陆到我们网站上的特定页面时,这尤其有帮助。
这在我们的主要routes.tsx
文件中。
useEffect(() => {
// The path that the user is supposed to go to
const destinationPath = location.pathname;
// If our site was opened in a new window or tab and accessed directly
// OR the page before this one was NOT a page on our site, then...
if (
document.referrer === window.location.href ||
document.referrer.indexOf(window.location.host) === -1
) {
// Replaces the current pathname with the homepage
history.replace("/");
// Then pushes to the path the user was supposed to go to
history.push(destinationPath);
}
}, []);
现在,当用户按下返回按钮时,它会将用户带回我们的主页,而不是被困在他们所在的“嵌套”路由中。
注意:这种实现方式存在一些小问题。我们的应用程序也是Cordova应用程序,因此我们需要有自己的返回按钮。这种实现方式可以很好地与我们自己的返回按钮配合使用,但似乎无法与本机浏览器的返回按钮配合使用;因此,它对我们的需求非常有效。
浏览器的返回按钮是基于你的路由历史记录工作的。它不会调用你程序化处理的路由。这就是我们应该使用React Router来保持历史堆栈的地方。如果你在路由'/'
,然后推入'/home'
。当你按下浏览器的返回按钮时,它将弹出'/home'
并返回到'/'
。
即使你实现了Button
组件来实现返回功能,并使用React Router的history props
。相信我,你必须仔细管理你的导航,以适当地维护浏览器历史堆栈。这样它才能像按下浏览器的返回按钮或你的应用程序Button
一样正常工作。希望这对你有所帮助。
你可以使用 ../
或者 useNavigate
来实现这个功能。
import { Link } from "react-router-dom";
.
.
.
<Link to="../" relative="path"> Back </Link>
或者(推荐)
import { useNavigate} from "react-router-dom";
.
.
.
const navigate = useNavigate();
.
.
.
<button onClick={() => navigate(-1)}> Back </button>
源代码: React Router v6.11.1