- withRouter已经在react-router-dom v6版本中废弃,取而代之的是useNavigate
import { useNavigate } from "react-router-dom";
let navigate = useNavigate();
function navigateToDetail(id){
navigate(`detail/${id}`)
}
- 在detail页面需要使用useParams接收路由params参数
import {useParams} from "react-router-dom";
const {id} = useParams();
- 如果传递的是search参数(例如:detail?id=1&name=李四)需要使用useSearchParams获取search参数
import {useSearchParams} from "react-router-dom"
const [searchParams] = useSearchParams();
const id = searchParams.get("id");
- 如果传递的是state参数需要使用useLocation获取参数
import {useLocation} from "react-router-dom";
const location = useLocation();
const {id} = location.state;