- withRouter已經(jīng)在react-router-dom v6版本中廢棄肄方,取而代之的是useNavigate
import { useNavigate } from "react-router-dom";
let navigate = useNavigate();
function navigateToDetail(id){
navigate(`detail/${id}`)
}
- 在detail頁面需要使用useParams接收路由params參數(shù)
import {useParams} from "react-router-dom";
const {id} = useParams();
- 如果傳遞的是search參數(shù)(例如:detail?id=1&name=李四)需要使用useSearchParams獲取search參數(shù)
import {useSearchParams} from "react-router-dom"
const [searchParams] = useSearchParams();
const id = searchParams.get("id");
- 如果傳遞的是state參數(shù)需要使用useLocation獲取參數(shù)
import {useLocation} from "react-router-dom";
const location = useLocation();
const {id} = location.state;