当前位置: 首页 > Web前端 > HTML

React【包括V5.x、V6.x】中使用react-router-dom编程式路由导航的正确姿势!

时间:2023-03-28 13:49:55 HTML

react-router-dom程序化路由导航(v5)1.push跳转+携带params参数props.history.push(`/b/child1/${id}/${title}`);2.push跳转+携带搜索参数props.history.push(`/b/child1?id=${id}&title=${title}`);3.push跳转+携带状态参数props.history.push(`/b/child1`,{id,title});4.替换跳转+携带params参数this.props.history.replace(`/home/message/detail/${id}/${title}`)5.替换跳转+携带搜索参数this.props.history.replace(`/home/message/detail?id=${id}&title=${title}`)6.替换跳转+携带state参数this.props.history。替换(`/home/message/detail`,{id,title});7.转发this.props.history.goForward();8.回滚this.props.history.goForward();9.前进或后退Back(go)this.props.history.go(-2);//回到前2条路由在一般组件(非路由组件)中使用编程式路由导航import{withRouter}from'react-router-dom'classHeaderextendsComponent{//withRouter(Header)之后,可以使用这个.props.historyinsidethegeneralcomponent//...}exportdefaultwithRouter(Header)react-router-domprogrammaticroutingnavigation(v6)//v6版本编程导航使用useNavigate(以下为导入代码)import{useNavigate}来自“react-router-dom”;出口默认函数A(){constnavigate=useNavigate();//...}1。pushjump+携带params参数navigate(`/b/child1/${id}/${title}`);2.推跳+携带搜索参数navigate(`/b/child2?id=${id}&title=${title}`);3.push跳转+携带状态参数navigate("/b/child2",{state:{id,title}});4.替换跳转+携带params参数navigate(`/b/child1/${id}/${title}`,{replace:true});5.replacejump+携带搜索参数navigate(`/b/child2?id=${id}&title=${title}`,{replace:true});6.替换跳转+携带状态参数navigate("/b/child2",{state:{id,title},replace:true});为您推荐相关文章:深入解析ReactuseRefHook的使用!https://juejin.cn/post/704258...最简洁的介绍Mbox6.x的基本使用步骤(仅三步)!!!https://juejin.cn/post/704110...(干货)全网最全react-router-domv6.0学习指南(新特性深度解读,持续更新中...)!!!https://juejin.cn/post/704028...(原创)深入解读React中修改值的useStateHook,但不会重新渲染或刷新的问题https://juejin.cn/post/703923...React中react-router-dom路由参数的三种使用方法详解【含V5.x、V6.x】!!!https://juejin.cn/post/704284...