react-router-dom编程式路由导航
react-router-dom编程式路由导航
V5版本
1.push跳转+携带params参数
props.history.push(`/b/childl/${id}/${title}`);
2.push跳转+携带search参数
props.history.push(`/b/childl?id=${id}&title=${title}`);
3.push跳转+携带state参数
props.history.push(`/b/child`,{id,title});
4.replace跳转+携带params参数
this.props.history.replace(`/home/message/detail/${id}/${title}`)
5.replace跳转+携带search参数
this.props.history.replace(`/home/message/detail?id=${id}&title=${title}`)
6.replace跳转+携带state参数
this.props.history.replace(`/home/message/detail`,{id,title});
7.前进
this.props.history.goForward();
8.回退
this.props.history.goForward();
9.前进或回退(go)
this.props.history.go(-2);//回退到前2条的路由
在一般组件中使用编程式路由导航(费路由组件)
import {withRouter} from 'react-router-dom'
class Header extends Component {
// withRouter(Header)后,就可以在一般组件内部使用 this.props.history
}
export default withRouter(Header)
V6版本
// v6版本编程导航使用useNavigate(以下为引入代码)
import {useNavigate} from 'react-router-dom';
export default function A(){
const navigate = useNavigate();
}
1.push跳转+携带params参数
navigate(`/b/childl/${id}/${title}`);
2.push跳转+携带search参数
navigate(`/b/child2?id${id}&title=${title}`);
3.push跳转+携带state参数
navigate("/b/child2",{state:{id,title}});
4.replace跳转+携带params参数
navigate(`/b/childl/${id}/${title}`,{replace:true});
5.replace跳转+携带search参数
navigate(`/b/child2?id=${id}&title=${title}`,{replace:true});
6.replace跳转+携带state参数
navigate('/b/child2',{state:{id,title},replace:true})
标签:
React
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南