Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tailwind react location #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix useRouter hook
nejcrogelsek committed May 26, 2022
commit 4ef12c83f61a5ae0215a315a27d3c7f080ab65e7
2 changes: 1 addition & 1 deletion src/components/ConfirmationModal/ConfirmationModal.tsx
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ const ConfirmationModal: FC<Props> = ({ confirmationData }: Props) => {

useEffect(() => {
cancel()
}, [router.location.pathname])
}, [router.location.current.pathname])

const confirm = () => {
if (confirmationData.onConfirm) {
14 changes: 7 additions & 7 deletions src/lib/hooks/useQueryParameters.ts
Original file line number Diff line number Diff line change
@@ -62,17 +62,17 @@ const mergeValues = (
}
export function useQueryParameters(): QueryParameters {
const router = useRouter()
const values = qs.parse(router.location.search, qsOptions)
const hash = router.location.hash.length > 1 ? router.location.hash : undefined
const values = qs.parse(router.location.current.search.toString(), qsOptions)
const hash = router.location.current.hash.length > 1 ? router.location.current.hash : undefined
const getUrlWithQueryParams = (fields: string | { [field: string]: FilterValue }, value?: FilterValue) => {
const newValues = mergeValues(values, fields, value)
const queryString = qs.stringify(newValues, qsOptions)
const url = `${router.location.pathname}?${queryString}${hash || ''}`
const url = `${router.location.current.pathname}?${queryString}${hash || ''}`
return url
}
const set: QueryParameters['set'] = (fields: string | { [field: string]: FilterValue }, value?: FilterValue) => {
const url = getUrlWithQueryParams(fields, value)
router.navigate(url, { replace: true })
router.navigate({ to: url, replace: true })
}
function get(name: string): string | undefined {
const val = values[name]
@@ -102,11 +102,11 @@ export function useQueryParameters(): QueryParameters {
}
function setHash(value: string | undefined) {
const suffix = value ? `#${value}` : ''
const url = `${router.location.pathname}${router.location.search}${suffix}`
router.navigate(url, { replace: true })
const url = `${router.location.current.pathname}${router.location.current.search}${suffix}`
router.navigate({ to: url, replace: true })
}
return {
path: router.location.pathname,
path: router.location.current.pathname,
hash,
getNumber,
get,
4 changes: 1 addition & 3 deletions src/lib/hooks/useRouter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useNavigate, useLocation, useParams } from 'react-router-dom'
import { useNavigate, useLocation } from '@tanstack/react-location'

export function useRouter() {
const navigate = useNavigate()
const location = useLocation()
const params = useParams()

return {
navigate,
location,
params,
}
}
2 changes: 1 addition & 1 deletion src/lib/hooks/useScrollToTop.ts
Original file line number Diff line number Diff line change
@@ -4,5 +4,5 @@ export const useScrollToTop = () => {
const router = useRouter()
useEffect(() => {
window.scrollTo(0, 0)
}, [router.location.pathname])
}, [router.location.current.pathname])
}