React Router 如何使用history跳转的实现
导读:收集整理的这篇文章主要介绍了React Router 如何使用history跳转的实现,觉得挺不错的,现在分享给大家,也给大家做个参考。 在react-router中组件里面的跳转可以用...
收集整理的这篇文章主要介绍了React Router 如何使用history跳转的实现,觉得挺不错的,现在分享给大家,也给大家做个参考。 在react-router中组件里面的跳转可以用Link>
但是在组件外面改如何跳转,需要用到react路由的history
replace方法和push方法使用形式一样,replace的作用是取代当前历史记录
go,此方法用来前进或者倒退,history.go(-1);
goBack,此方法用来回退,history.goBack();
goForward,此方法用来前进,history.goForward();
1.hook
import { useHistory} From 'react-router-dom'; function goPage(e) { history.push({ pathname: "/home", state: { id: 1} } ); }
pathname是路由地址,state可以传参
获取参数:
import { useLocation} from 'react-router-dom'; function getParams(){ let location = useLocation(); let id = location.state.id; }
2.class组件
import React from 'react'; import { createbrowserHistory} from "history"; class App extends React.component{ constructor(PRops) { suPEr(props); } goPage() { let history = createBrowserHistory() history.push({ pathname: "/home", state: { id: 1} } ); history.go(); } render() { return null; } }
如果不调用history.go则路由改变了,但是页面不会跳转。
到此这篇关于React Router 如何使用history跳转的实现的文章就介绍到这了,更多相关React Router history跳转内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:- react-router v4如何使用history控制路由跳转详解
- react-router JS 控制路由跳转实例
- react-router实现跳转传值的方法示例
- React Router 5.1.0使用useHistory做页面跳转导航的实现
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: React Router 如何使用history跳转的实现
本文地址: https://pptw.com/jishu/595086.html