reactjs - React router 6,使用Navigate如何获取路径名
问题描述
我试图了解如何使用 react router 6 和 useNavigate 但无法弄清楚如何获取当前路径名。
使用 useHistory 我可以history.location.pathname
用来获取当前的 url。但是我如何使用 useNavigate 来做同样的事情呢?
import React, {useState} from 'react'
import styed from 'styled-components'
import { useNavigate } from 'react-router-dom'
import { MovieState } from '../movieState'
export const MovieDetail = () => {
let navigate = useNavigate();
const [movies, setMovies] = useState(MovieState)
const [movie, setMovie] = useState(null)
//const url = navigate.location.pathname;
return (
<div>
<h1>MovieDetail</h1>
</div>
)
}
解决方案
如果您需要访问 location.pathname,请使用useLocation挂钩:
import { useLocation } from 'react-router-dom';
const App = () => {
const location = useLocation();
console.log(location.pathname)
//...
};
推荐阅读
- javascript - 当字体名称有空格时如何使用 JQuery 设置 CSS 字体系列
- google-bigquery - google bigquery 提取压缩现在不起作用
- javascript - 如何在html中获取js返回值请在屏幕截图中找到以下代码
- java - 如果我们没有为对象创建引用变量,如何关闭扫描程序类,从而避免资源泄漏警告消息
- r - 箱线图轴和分类变量
- angular - angular2-fixture.nativeElement.query 中的 ag-grid 单元测试不是函数
- javascript - Yii2-获取控制台中的类型和引用错误
- solr - Sitecore solr 按字符而不是单词搜索
- objective-c - Collectionview 在 Apple TV 的右侧显示虚线页面指示符
- javascript - 每次成功拖放后一张一张加载图像以进行拖动