如何在React Router v5中推送到历史记录? [英] How to push to History in React Router v5?

查看:140
本文介绍了如何在React Router v5中推送到历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个全局历史文件来管理react-router-dom v5上的createBrowserHistory()? 我知道V5具有useHistory()作为获取历史记录的一种方式.但是是否可以从任何地方检索历史记录,例如在我不使用功能组件的情况下?

Is it possible to create a global history file to manage the createBrowserHistory() on react-router-dom v5? I know the V5 has the useHistory() as a way to get the history. But is it possible to retrieve the history from anywhere, like for cases where I am not using a function component?

在V4上,我可以创建一个文件history.js:

On V4 I could create a file history.js:

import { createBrowserHistory } from 'history';
export default createBrowserHistory();

适用于V4 https://codesandbox.io/s/react-router-v4-nfwr0

在V5上不起作用-更新URL,但重定向到未找到 https://codesandbox.io/s/react-router-v5- not-working-jlrep

It doesn't work on V5 - It updates the URL but redirects to not-found https://codesandbox.io/s/react-router-v5-not-working-jlrep

推荐答案

正如文档所说,您应该使用历史记录的v4来处理react-router v5.

As the doc says you should use the v4 of history to work on react-router v5.

https://github.com/ReactTraining/history

版本4的文档可以在v4分支上找到.版本4用于React Router版本4和5.

Documentation for version 4 can be found on the v4 branch. Version 4 is used in React Router versions 4 and 5.

这篇关于如何在React Router v5中推送到历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆