I'm currently working on a React project with react-router-dom v6 and I want to get all of the query parameters.
http://localhost:3000/users?page=5&pageSize=25
I want to get both page and pageSize. I know that we can use this code below to get params with the keys.
import React from 'react'
import {useSearchParams} from "react-router-dom";
const Users = () => {
const [searchParams, setSearchParams] = useSearchParams();
const page = searchParams.get('page')
const pageSize = searchParams.get('pageSize')
return (<h1>page: {page}, pageSize: {pageSize}</h1>)
}
But, then I tried to get all params without specifying the keys by using searchparams.getAll()
but it didn't work and the React app showed only a blank page.
Here is my code I used to get all params:
import React from 'react'
import {useSearchParams} from "react-router-dom";
const Users = () => {
const [searchParams, setSearchParams] = useSearchParams();
const params = searchParams.getAll();
console.log(params)
return (<h1>params</h1>)
}
Did I make any mistake there?
This is my dependencies on package.json
:
"dependencies": {
...,
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.2",
...,
},