How can I get the url params from a route?
I got this code:
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route path="/table/:name/" element={<SortingTable/>}></Route>
<Route path="" exact element={<BasicTable/>}></Route>
</Routes>
</BrowserRouter>
</div>
);
}
I want to get the name
from the url and use it in SortingTable:
const SortingTable = ({match}) => {
let [searchParams, setSearchParams] = useSearchParams();
const [data, setData] = useState([])
const [columns, setColumns] = useState([])
const table = match.params.name //=> undefined
2
Answers
use useParams from React Router
You can use the
useParams
:Note: When you want to get the queries from the URL, you can use the
useSearchParams
, But when you want to get the params you defined in yourRoute
component andpath
prop, you can use theuseParams