Skip to content

Commit bf8fad2

Browse files
feat!: support react-router-dom v6 (#479)
* feat!: support react-router-dom v6 * fix: remove only * fix: remove unused comment Co-authored-by: Renovate Bot <[email protected]>
1 parent a69b435 commit bf8fad2

File tree

4 files changed

+62
-114
lines changed

4 files changed

+62
-114
lines changed

packages/use-query-params/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
},
2929
"license": "MIT",
3030
"dependencies": {
31-
"history": "4.10.1",
31+
"history": "5.1.0",
3232
"query-string": "7.0.1",
33-
"react-router-dom": "5.3.0"
33+
"react-router-dom": "6.0.1"
3434
},
3535
"peerDependencies": {
3636
"react": "17.x",
3737
"react-dom": "17.x",
38-
"react-router-dom": "^5.2.0"
38+
"react-router-dom": "^6.0.0"
3939
}
4040
}

packages/use-query-params/src/__tests__/index.tsx

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
import { act, renderHook } from '@testing-library/react-hooks'
22
import { History, createMemoryHistory } from 'history'
3-
import React, { ReactNode } from 'react'
3+
import React, { ReactNode, useLayoutEffect, useState } from 'react'
44
import { MemoryRouter, Router } from 'react-router-dom'
55
import useQueryParams from '..'
66

77
const wrapper =
8-
({ pathname = 'one', search, history }: { pathname?: string, search: string, history?: History }) =>
9-
// eslint-disable-next-line react/prop-types
10-
({ children }: { children: ReactNode }) =>
11-
(
12-
history ? (
13-
<Router history={history}>
8+
({ pathname = '/one', search, history }: { pathname?: string, search: string, history?: History }) =>
9+
({ children }: { children: ReactNode }) => {
10+
11+
if (history) {
12+
const [state, setState] = useState({
13+
action: history.action,
14+
location: history.location
15+
});
16+
17+
useLayoutEffect(() => history.listen(setState), []);
18+
19+
return (
20+
<Router navigator={history} location={state.location} navigationType={state.action}>
1421
{children}
1522
</Router>
16-
) : (
17-
<MemoryRouter initialIndex={0} initialEntries={[{ pathname, search }]}>
18-
{children}
19-
</MemoryRouter>
2023
)
24+
}
25+
26+
return (
27+
<MemoryRouter initialIndex={0} initialEntries={[{ pathname, search }]}>
28+
{children}
29+
</MemoryRouter>
2130
)
31+
}
32+
2233

2334
describe('useQueryParam', () => {
2435
it('should correctly push instead of replacing history', () => {
2536
const history = createMemoryHistory({
26-
initialEntries: ['user=john'],
27-
initialIndex: 0,
37+
initialEntries: ['/one?user=john'],
38+
initialIndex: 1,
2839
})
2940

3041
const { result } = renderHook(() => useQueryParams(), {
@@ -35,17 +46,17 @@ describe('useQueryParam', () => {
3546
result.current.setQueryParams({ user: 'John' })
3647
})
3748
expect(result.current.queryParams).toEqual({ user: 'John' })
38-
expect(history.length).toBe(1)
49+
expect(history.index).toBe(0)
3950
act(() => {
4051
result.current.setQueryParams({ user: 'Jack' })
4152
})
4253
expect(result.current.queryParams).toEqual({ user: 'Jack' })
43-
expect(history.length).toBe(1)
54+
expect(history.index).toBe(0)
4455
act(() => {
4556
result.current.setQueryParams({ user: 'Jerry' }, { push: true })
4657
})
4758
expect(result.current.queryParams).toEqual({ user: 'Jerry' })
48-
expect(history.length).toBe(2)
59+
expect(history.index).toBe(1)
4960
})
5061

5162
it('should set one object', () => {

packages/use-query-params/src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { History } from 'history'
21
import { ParsedQuery, parse, stringify } from 'query-string'
32
import { useCallback, useMemo } from 'react'
4-
import { useHistory, useLocation } from 'react-router-dom'
3+
import { useLocation, useNavigate } from 'react-router-dom'
54

65
interface Options {
76
/** Set to true to push a new entry onto the history stack */
@@ -25,10 +24,8 @@ const useQueryParams = (): {
2524
*/
2625
setQueryParams: typeof setQueryParams
2726
} => {
28-
// eslint-disable-next-line @typescript-eslint/unbound-method
29-
const { replace, push } = useHistory<History>()
30-
// eslint-disable-next-line @typescript-eslint/unbound-method
31-
const location = useLocation<History>()
27+
const navigate = useNavigate()
28+
const location = useLocation()
3229

3330
const currentState = useMemo(
3431
() =>
@@ -57,11 +54,10 @@ const useQueryParams = (): {
5754
const searchToCompare = location.search || '?'
5855

5956
if (searchToCompare !== `?${stringifiedParams}`) {
60-
const fn = options?.push ? push : replace
61-
fn(`${location.pathname}?${stringifiedParams}`)
57+
navigate(`${location.pathname}?${stringifiedParams}`, { replace: !options?.push })
6258
}
6359
},
64-
[push, replace, location.pathname, location.search, stringyFormat],
60+
[navigate, location.pathname, location.search, stringyFormat],
6561
)
6662

6763
const setQueryParams = useCallback(

yarn.lock

Lines changed: 27 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -957,10 +957,10 @@
957957
core-js-pure "^3.0.0"
958958
regenerator-runtime "^0.13.4"
959959

960-
"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.13", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
961-
version "7.15.4"
962-
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
963-
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
960+
"@babel/runtime@^7.10.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
961+
version "7.16.0"
962+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.16.0.tgz#e27b977f2e2088ba24748bf99b5e1dece64e4f0b"
963+
integrity sha512-Nht8L0O8YCktmsDV6FqFue7vQLRx3Hb0B37lS5y0jDRqRxlBG4wIJHnf9/bgSE2UyipKFA01YtS+npRdTWBUyw==
964964
dependencies:
965965
regenerator-runtime "^0.13.4"
966966

@@ -4780,19 +4780,14 @@ has@^1.0.3:
47804780
dependencies:
47814781
function-bind "^1.1.1"
47824782

4783-
history@4.10.1, history@^4.9.0:
4784-
version "4.10.1"
4785-
resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3"
4786-
integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==
4783+
history@5.1.0, history@^5.1.0:
4784+
version "5.1.0"
4785+
resolved "https://registry.yarnpkg.com/history/-/history-5.1.0.tgz#2e93c09c064194d38d52ed62afd0afc9d9b01ece"
4786+
integrity sha512-zPuQgPacm2vH2xdORvGGz1wQMuHSIB56yNAy5FnLuwOwgSYyPKptJtcMm6Ev+hRGeS+GzhbmRacHzvlESbFwDg==
47874787
dependencies:
4788-
"@babel/runtime" "^7.1.2"
4789-
loose-envify "^1.2.0"
4790-
resolve-pathname "^3.0.0"
4791-
tiny-invariant "^1.0.2"
4792-
tiny-warning "^1.0.0"
4793-
value-equal "^1.0.1"
4788+
"@babel/runtime" "^7.7.6"
47944789

4795-
hoist-non-react-statics@^3.1.0, hoist-non-react-statics@^3.3.1:
4790+
hoist-non-react-statics@^3.3.1:
47964791
version "3.3.2"
47974792
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
47984793
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
@@ -5219,11 +5214,6 @@ is-wsl@^2.1.1:
52195214
dependencies:
52205215
is-docker "^2.0.0"
52215216

5222-
5223-
version "0.0.1"
5224-
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
5225-
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=
5226-
52275217
isarray@~1.0.0:
52285218
version "1.0.0"
52295219
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
@@ -6092,7 +6082,7 @@ log-update@^4.0.0:
60926082
slice-ansi "^4.0.0"
60936083
wrap-ansi "^6.2.0"
60946084

6095-
loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
6085+
loose-envify@^1.1.0, loose-envify@^1.4.0:
60966086
version "1.4.0"
60976087
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
60986088
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
@@ -6272,14 +6262,6 @@ min-indent@^1.0.0:
62726262
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
62736263
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
62746264

6275-
mini-create-react-context@^0.4.0:
6276-
version "0.4.1"
6277-
resolved "https://registry.yarnpkg.com/mini-create-react-context/-/mini-create-react-context-0.4.1.tgz#072171561bfdc922da08a60c2197a497cc2d1d5e"
6278-
integrity sha512-YWCYEmd5CQeHGSAKrYvXgmzzkrvssZcuuQDDeqkT+PziKGMgE+0MCCtcKbROzocGBG1meBLl2FotlRwf4gAzbQ==
6279-
dependencies:
6280-
"@babel/runtime" "^7.12.1"
6281-
tiny-warning "^1.0.3"
6282-
62836265
minimatch@^3.0.4:
62846266
version "3.0.4"
62856267
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
@@ -7013,13 +6995,6 @@ path-parse@^1.0.6:
70136995
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
70146996
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
70156997

7016-
path-to-regexp@^1.7.0:
7017-
version "1.8.0"
7018-
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.8.0.tgz#887b3ba9d84393e87a0a0b9f4cb756198b53548a"
7019-
integrity sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==
7020-
dependencies:
7021-
isarray "0.0.1"
7022-
70236998
path-type@^3.0.0:
70246999
version "3.0.0"
70257000
resolved "https://registry.yarnpkg.com/path-type/-/path-type-3.0.0.tgz#cef31dc8e0a1a3bb0d105c0cd97cf3bf47f4e36f"
@@ -7168,7 +7143,7 @@ promzard@^0.3.0:
71687143
dependencies:
71697144
read "1"
71707145

7171-
[email protected], prop-types@^15.6.2, prop-types@^15.7.2:
7146+
[email protected], prop-types@^15.7.2:
71727147
version "15.7.2"
71737148
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
71747149
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
@@ -7260,7 +7235,7 @@ react-error-boundary@^3.1.0:
72607235
dependencies:
72617236
"@babel/runtime" "^7.12.5"
72627237

7263-
react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1:
7238+
react-is@^16.7.0, react-is@^16.8.1:
72647239
version "16.13.1"
72657240
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
72667241
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
@@ -7270,34 +7245,20 @@ react-is@^17.0.1:
72707245
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339"
72717246
integrity sha512-NAnt2iGDXohE5LI7uBnLnqvLQMtzhkiAOLXTmv+qnF9Ky7xAPcX8Up/xWIhxvLVGJvuLiNc4xQLtuqDRzb4fSA==
72727247

7273-
7274-
version "5.3.0"
7275-
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-5.3.0.tgz#da1bfb535a0e89a712a93b97dd76f47ad1f32363"
7276-
integrity sha512-ObVBLjUZsphUUMVycibxgMdh5jJ1e3o+KpAZBVeHcNQZ4W+uUGGWsokurzlF4YOldQYRQL4y6yFRWM4m3svmuQ==
7277-
dependencies:
7278-
"@babel/runtime" "^7.12.13"
7279-
history "^4.9.0"
7280-
loose-envify "^1.3.1"
7281-
prop-types "^15.6.2"
7282-
react-router "5.2.1"
7283-
tiny-invariant "^1.0.2"
7284-
tiny-warning "^1.0.0"
7285-
7286-
7287-
version "5.2.1"
7288-
resolved "https://registry.yarnpkg.com/react-router/-/react-router-5.2.1.tgz#4d2e4e9d5ae9425091845b8dbc6d9d276239774d"
7289-
integrity sha512-lIboRiOtDLFdg1VTemMwud9vRVuOCZmUIT/7lUoZiSpPODiiH1UQlfXy+vPLC/7IWdFYnhRwAyNqA/+I7wnvKQ==
7290-
dependencies:
7291-
"@babel/runtime" "^7.12.13"
7292-
history "^4.9.0"
7293-
hoist-non-react-statics "^3.1.0"
7294-
loose-envify "^1.3.1"
7295-
mini-create-react-context "^0.4.0"
7296-
path-to-regexp "^1.7.0"
7297-
prop-types "^15.6.2"
7298-
react-is "^16.6.0"
7299-
tiny-invariant "^1.0.2"
7300-
tiny-warning "^1.0.0"
7248+
7249+
version "6.0.1"
7250+
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-6.0.1.tgz#958d5deac8932ce209001ba7a11d5ae605148856"
7251+
integrity sha512-fiE+PzFTrof5q8Z/+RHzuiin9/U/q5KY2adlHClwYexbY0DqJnHcC/0U9yv3Amz9em2/bcK7X8mk7+zxB+qhvg==
7252+
dependencies:
7253+
history "^5.1.0"
7254+
react-router "6.0.1"
7255+
7256+
7257+
version "6.0.1"
7258+
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.0.1.tgz#3fe93ad11f91fb55e242a42628414cb47219e652"
7259+
integrity sha512-O3iab52icFnQaHWONZr50CcjRlf3gx8CCjPQ0YxN8xEuEklRJNgoZSeoYFYz0fLvA4cpnhc306Nd8BYgL4QZrQ==
7260+
dependencies:
7261+
history "^5.1.0"
73017262

73027263
73037264
version "17.0.2"
@@ -7568,11 +7529,6 @@ [email protected], resolve-global@^1.0.0:
75687529
dependencies:
75697530
global-dirs "^0.1.1"
75707531

7571-
resolve-pathname@^3.0.0:
7572-
version "3.0.0"
7573-
resolved "https://registry.yarnpkg.com/resolve-pathname/-/resolve-pathname-3.0.0.tgz#99d02224d3cf263689becbb393bc560313025dcd"
7574-
integrity sha512-C7rARubxI8bXFNB/hqcp/4iUeIXJhJZvFPFPiSPRnhU5UPxzMFIl+2E6yY6c4k9giDJAhtV+enfA+G89N6Csng==
7575-
75767532
resolve.exports@^1.1.0:
75777533
version "1.1.0"
75787534
resolved "https://registry.yarnpkg.com/resolve.exports/-/resolve.exports-1.1.0.tgz#5ce842b94b05146c0e03076985d1d0e7e48c90c9"
@@ -8250,16 +8206,6 @@ through@2, "through@>=2.2.7 <3", through@^2.3.4, through@^2.3.6, through@^2.3.8:
82508206
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
82518207
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
82528208

8253-
tiny-invariant@^1.0.2:
8254-
version "1.1.0"
8255-
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.1.0.tgz#634c5f8efdc27714b7f386c35e6760991d230875"
8256-
integrity sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==
8257-
8258-
tiny-warning@^1.0.0, tiny-warning@^1.0.3:
8259-
version "1.0.3"
8260-
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
8261-
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==
8262-
82638209
tmp@^0.0.33:
82648210
version "0.0.33"
82658211
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
@@ -8580,11 +8526,6 @@ validate-npm-package-name@^3.0.0:
85808526
dependencies:
85818527
builtins "^1.0.3"
85828528

8583-
value-equal@^1.0.1:
8584-
version "1.0.1"
8585-
resolved "https://registry.yarnpkg.com/value-equal/-/value-equal-1.0.1.tgz#1e0b794c734c5c0cade179c437d356d931a34d6c"
8586-
integrity sha512-NOJ6JZCAWr0zlxZt+xqCHNTEKOsrks2HQd4MqhP1qy4z1SkbEP467eNx6TgDKXMvUOb+OENfJCZwM+16n7fRfw==
8587-
85888529
85898530
version "1.10.0"
85908531
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"

0 commit comments

Comments
 (0)