A small hook to handle params
npm install @scaleway/use-query-params@scaleway/use-query-params``bash`
$ pnpm add @scaleway/use-query-params react-router-dom
`js
import React from 'react'
import { render } from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
import useQueryParams from '@scaleway/use-query-params'
const App = () => {
const { queryParams, setQueryParams } = useQueryParams()
const { user } = queryParams
const setUser = () => setQueryParams({ user: 'John' })
// ?user=John
return (
<>
render(
document.getElementById('react-root'),
)
`
Merge current query params with the new ones passed in parameters.
`js?company=Scaleway".
// In this exemple we assume that we have an URL that include :
import React from 'react'
import useQueryParams from '@scaleway/use-query-params'
const Component = () => {
const { queryParams, setQueryParams } = useQueryParams()
const { user, company } = queryParams // user will be undefined and company will be "Scaleway"
const setUser = () => setQueryParams({ user: 'John' }) // user will be "John" and company will be "Scaleway"
// ?company=Scaleway&user=John
return (
<>
$3
Erase current query params and replace by the new ones passed in parameters.
`js
// In this exemple we assume that we have an URL that include : ?company=Scaleway".const Component = () => {
const { queryParams, replaceQueryParams } = useQueryParams()
const { user, company } = queryParams // user will be undefined and company will be "Scaleway"
const setUser = () => replaceQueryParams({ user: 'John' }) // user will be "John" and company will be undefined
// ?user=John
return (
<>
$3
To avoid mutating history
`js
// In this exemple we assume that we have an URL that include : ?company=Scaleway".const Component = () => {
const { queryParams, replaceQueryParams } = useQueryParams()
const { user, company } = queryParams // user will be undefined and company will be "Scaleway"
const setUser = () => replaceQueryParams({ user: 'John' }, { push: true }) // user will be "John" and company will be undefined
// ?user=John
return (
<>