Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 9269f74

Browse files
committed
react-router-dom v6 support?
Fixes #172
1 parent 6dbc189 commit 9269f74

15 files changed

+102
-308
lines changed

example/src/App.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import UseQueryParamCode from '!!raw-loader!./UseQueryParam'
55
import UseRouteParamCode from '!!raw-loader!./UseRouteParam'
66
import { Box, Card, CardContent, CardMedia, Typography } from '@material-ui/core'
77
import React from 'react'
8-
import { BrowserRouter, Route, Switch } from 'react-router-dom'
8+
import { BrowserRouter, Route, Routes } from 'react-router-dom'
99
import SyntaxHighlighter from 'react-syntax-highlighter'
1010
import CreateLinkWithState from './CreateLinkWithState'
1111
import UseArrayQueryKey from './UseArrayQueryKey'
@@ -64,11 +64,11 @@ const App = () => {
6464

6565
return (
6666
<BrowserRouter basename='/use-route-as-state/'>
67-
<Switch>
67+
<Routes>
6868
<Route path='/'>
6969
<Examples />
7070
</Route>
71-
</Switch>
71+
</Routes>
7272
</BrowserRouter>
7373
)
7474
}

example/src/UseRouteParam.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Box, TextField, Typography } from '@material-ui/core'
22
import React from 'react'
3-
import { Route, Switch, useRouteMatch } from 'react-router-dom'
3+
import { Route, Routes, useRouteMatch } from 'react-router-dom'
44
import { useRouteParams } from 'use-route-as-state'
55

66
const UseRouteParam = () => {
@@ -18,9 +18,9 @@ const UseRouteParam = () => {
1818
export default () => {
1919
const { path } = useRouteMatch()
2020

21-
return <Switch>
21+
return <Routes>
2222
<Route path={`${path}:rParam?`}>
2323
<UseRouteParam />
2424
</Route>
25-
</Switch>
25+
</Routes>
2626
}

example/yarn.lock

+22-71
Original file line numberDiff line numberDiff line change
@@ -1165,20 +1165,20 @@
11651165
dependencies:
11661166
regenerator-runtime "^0.13.4"
11671167

1168-
"@babel/runtime@^7.1.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.13":
1169-
version "7.17.9"
1170-
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.9.tgz#d19fbf802d01a8cb6cf053a64e472d42c434ba72"
1171-
integrity sha512-lSiBBvodq29uShpWGNbgFdKYNiFDo5/HIYsaCEY9ff4sb10x9jizo2+pRrSyF4jKZCXqgzuqBOQKbUm90gQwJg==
1172-
dependencies:
1173-
regenerator-runtime "^0.13.4"
1174-
11751168
"@babel/runtime@^7.10.2", "@babel/runtime@^7.16.3", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.3", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7":
11761169
version "7.16.3"
11771170
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.16.3.tgz#b86f0db02a04187a3c17caa77de69840165d42d5"
11781171
integrity sha512-WBwekcqacdY2e9AF/Q7WLFUWmdJGJTkbjqTjoMDgXkVZ3ZRUvOPsLb5KdwISoQVsbP+DQzVZW4Zhci0DvpbNTQ==
11791172
dependencies:
11801173
regenerator-runtime "^0.13.4"
11811174

1175+
"@babel/runtime@^7.7.6":
1176+
version "7.17.9"
1177+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.9.tgz#d19fbf802d01a8cb6cf053a64e472d42c434ba72"
1178+
integrity sha512-lSiBBvodq29uShpWGNbgFdKYNiFDo5/HIYsaCEY9ff4sb10x9jizo2+pRrSyF4jKZCXqgzuqBOQKbUm90gQwJg==
1179+
dependencies:
1180+
regenerator-runtime "^0.13.4"
1181+
11821182
"@babel/template@^7.10.4", "@babel/template@^7.16.0", "@babel/template@^7.3.3":
11831183
version "7.16.0"
11841184
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.16.0.tgz#d16a35ebf4cd74e202083356fab21dd89363ddd6"
@@ -5570,17 +5570,12 @@ highlight.js@^10.1.1, highlight.js@~10.7.0:
55705570
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.7.3.tgz#697272e3991356e40c3cac566a74eef681756531"
55715571
integrity sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==
55725572

5573-
history@^4.9.0:
5574-
version "4.10.1"
5575-
resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3"
5576-
integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==
5573+
history@^5.2.0:
5574+
version "5.3.0"
5575+
resolved "https://registry.yarnpkg.com/history/-/history-5.3.0.tgz#1548abaa245ba47992f063a0783db91ef201c73b"
5576+
integrity sha512-ZqaKwjjrAYUYfLG+htGaIIZ4nioX2L70ZUMIFysS3xvBsSG4x/n1V6TXV3N8ZYNuFGlDirFg32T7B6WOUPDYcQ==
55775577
dependencies:
5578-
"@babel/runtime" "^7.1.2"
5579-
loose-envify "^1.2.0"
5580-
resolve-pathname "^3.0.0"
5581-
tiny-invariant "^1.0.2"
5582-
tiny-warning "^1.0.0"
5583-
value-equal "^1.0.1"
5578+
"@babel/runtime" "^7.7.6"
55845579

55855580
hmac-drbg@^1.0.1:
55865581
version "1.0.1"
@@ -5591,7 +5586,7 @@ hmac-drbg@^1.0.1:
55915586
minimalistic-assert "^1.0.0"
55925587
minimalistic-crypto-utils "^1.0.1"
55935588

5594-
hoist-non-react-statics@^3.1.0, hoist-non-react-statics@^3.3.2:
5589+
hoist-non-react-statics@^3.3.2:
55955590
version "3.3.2"
55965591
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
55975592
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
@@ -6327,11 +6322,6 @@ is-wsl@^2.1.1, is-wsl@^2.2.0:
63276322
dependencies:
63286323
is-docker "^2.0.0"
63296324

6330-
6331-
version "0.0.1"
6332-
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
6333-
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=
6334-
63356325
[email protected], isarray@^1.0.0, isarray@~1.0.0:
63366326
version "1.0.0"
63376327
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
@@ -7248,7 +7238,7 @@ loglevel@^1.6.8:
72487238
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.8.0.tgz#e7ec73a57e1e7b419cb6c6ac06bf050b67356114"
72497239
integrity sha512-G6A/nJLRgWOuuwdNuA6koovfEV1YpqqAG4pRUlFaz3jj2QNZ8M4vBqnVA+HBTmU/AMNUtlOsMmSpF6NyOjztbA==
72507240

7251-
loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
7241+
loose-envify@^1.1.0, loose-envify@^1.4.0:
72527242
version "1.4.0"
72537243
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
72547244
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
@@ -7452,14 +7442,6 @@ mimic-fn@^2.1.0:
74527442
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
74537443
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
74547444

7455-
mini-create-react-context@^0.4.0:
7456-
version "0.4.1"
7457-
resolved "https://registry.yarnpkg.com/mini-create-react-context/-/mini-create-react-context-0.4.1.tgz#072171561bfdc922da08a60c2197a497cc2d1d5e"
7458-
integrity sha512-YWCYEmd5CQeHGSAKrYvXgmzzkrvssZcuuQDDeqkT+PziKGMgE+0MCCtcKbROzocGBG1meBLl2FotlRwf4gAzbQ==
7459-
dependencies:
7460-
"@babel/runtime" "^7.12.1"
7461-
tiny-warning "^1.0.3"
7462-
74637445
74647446
version "0.11.3"
74657447
resolved "https://registry.yarnpkg.com/mini-css-extract-plugin/-/mini-css-extract-plugin-0.11.3.tgz#15b0910a7f32e62ffde4a7430cfefbd700724ea6"
@@ -8231,13 +8213,6 @@ [email protected]:
82318213
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
82328214
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
82338215

8234-
path-to-regexp@^1.7.0:
8235-
version "1.8.0"
8236-
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.8.0.tgz#887b3ba9d84393e87a0a0b9f4cb756198b53548a"
8237-
integrity sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==
8238-
dependencies:
8239-
isarray "0.0.1"
8240-
82418216
path-type@^4.0.0:
82428217
version "4.0.0"
82438218
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
@@ -9340,7 +9315,7 @@ react-error-overlay@^6.0.9:
93409315
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.9.tgz#3c743010c9359608c375ecd6bc76f35d93995b0a"
93419316
integrity sha512-nQTTcUu+ATDbrSD1BZHr5kgSD4oF8OFjxun8uAaL8RwPBacGBNPf/yAuVVdx17N8XNzRDMrZ9XcKZHCjPW+9ew==
93429317

9343-
react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1:
9318+
react-is@^16.7.0, react-is@^16.8.1:
93449319
version "16.13.1"
93459320
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
93469321
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
@@ -9359,21 +9334,12 @@ react-refresh@^0.8.3:
93599334
version "0.0.0"
93609335
uid ""
93619336

9362-
9363-
version "5.2.1"
9364-
resolved "https://registry.yarnpkg.com/react-router/-/react-router-5.2.1.tgz#4d2e4e9d5ae9425091845b8dbc6d9d276239774d"
9365-
integrity sha512-lIboRiOtDLFdg1VTemMwud9vRVuOCZmUIT/7lUoZiSpPODiiH1UQlfXy+vPLC/7IWdFYnhRwAyNqA/+I7wnvKQ==
9366-
dependencies:
9367-
"@babel/runtime" "^7.12.13"
9368-
history "^4.9.0"
9369-
hoist-non-react-statics "^3.1.0"
9370-
loose-envify "^1.3.1"
9371-
mini-create-react-context "^0.4.0"
9372-
path-to-regexp "^1.7.0"
9373-
prop-types "^15.6.2"
9374-
react-is "^16.6.0"
9375-
tiny-invariant "^1.0.2"
9376-
tiny-warning "^1.0.0"
9337+
9338+
version "6.3.0"
9339+
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.3.0.tgz#3970cc64b4cb4eae0c1ea5203a80334fdd175557"
9340+
integrity sha512-7Wh1DzVQ+tlFjkeo+ujvjSqSJmkt1+8JO+T5xklPlgrh70y7ogx75ODRW0ThWhY7S+6yEDks8TYrtQe/aoboBQ==
9341+
dependencies:
9342+
history "^5.2.0"
93779343

93789344
react-scripts@^4.0.1:
93799345
version "4.0.3"
@@ -9698,11 +9664,6 @@ resolve-from@^5.0.0:
96989664
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
96999665
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==
97009666

9701-
resolve-pathname@^3.0.0:
9702-
version "3.0.0"
9703-
resolved "https://registry.yarnpkg.com/resolve-pathname/-/resolve-pathname-3.0.0.tgz#99d02224d3cf263689becbb393bc560313025dcd"
9704-
integrity sha512-C7rARubxI8bXFNB/hqcp/4iUeIXJhJZvFPFPiSPRnhU5UPxzMFIl+2E6yY6c4k9giDJAhtV+enfA+G89N6Csng==
9705-
97069667
resolve-url-loader@^3.1.2:
97079668
version "3.1.4"
97089669
resolved "https://registry.yarnpkg.com/resolve-url-loader/-/resolve-url-loader-3.1.4.tgz#3c16caebe0b9faea9c7cc252fa49d2353c412320"
@@ -10797,12 +10758,7 @@ timsort@^0.3.0:
1079710758
resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4"
1079810759
integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q=
1079910760

10800-
tiny-invariant@^1.0.2:
10801-
version "1.2.0"
10802-
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.2.0.tgz#a1141f86b672a9148c72e978a19a73b9b94a15a9"
10803-
integrity sha512-1Uhn/aqw5C6RI4KejVeTg6mIS7IqxnLJ8Mv2tV5rTc0qWobay7pDUz6Wi392Cnc8ak1H0F2cjoRzb2/AW4+Fvg==
10804-
10805-
tiny-warning@^1.0.0, tiny-warning@^1.0.2, tiny-warning@^1.0.3:
10761+
tiny-warning@^1.0.2:
1080610762
version "1.0.3"
1080710763
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
1080810764
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==
@@ -11223,11 +11179,6 @@ validate-npm-package-license@^3.0.1:
1122311179
spdx-correct "^3.0.0"
1122411180
spdx-expression-parse "^3.0.0"
1122511181

11226-
value-equal@^1.0.1:
11227-
version "1.0.1"
11228-
resolved "https://registry.yarnpkg.com/value-equal/-/value-equal-1.0.1.tgz#1e0b794c734c5c0cade179c437d356d931a34d6c"
11229-
integrity sha512-NOJ6JZCAWr0zlxZt+xqCHNTEKOsrks2HQd4MqhP1qy4z1SkbEP467eNx6TgDKXMvUOb+OENfJCZwM+16n7fRfw==
11230-
1123111182
vary@~1.1.2:
1123211183
version "1.1.2"
1123311184
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@
6666
"eslint": "^7.9.0",
6767
"eslint-friendly-formatter": "^4.0.1",
6868
"eslint-plugin-react-hooks": "^4.1.2",
69-
"history": "^4.0.0",
7069
"react": "^17.0.1",
71-
"react-router-dom": "^5.2.0",
70+
"react-router-dom": "^6.3.0",
7271
"react-scripts": "^4.0.1",
7372
"react-test-renderer": "^17.0.1",
7473
"rollup": "^2.26.9",

src/deprecated/encodeDecode.ts

-32
This file was deleted.

src/deprecated/helpers.ts

-21
This file was deleted.

src/deprecated/index.ts

-50
This file was deleted.

src/deprecated/types.ts

-1
This file was deleted.

src/encodeDecode.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMemo } from "react"
2-
import { useLocation, useRouteMatch } from "react-router-dom"
2+
import { useLocation, useMatch } from "react-router-dom"
33
import { getQueryParamsAsObject, removeUndefined } from "./helpers"
44

55
export const useDecodedLocation = () => {
@@ -11,24 +11,25 @@ export const useDecodedLocation = () => {
1111
}
1212

1313
export const useDecodedRouteMatch = () => {
14-
const { params, ...rest } = useRouteMatch()
14+
const match = useMatch('*')
15+
const { params, path } = useMemo(() => ({ params: match?.params || {}, path: match?.pathname }), [match])
1516

16-
const decodedParams = useMemo(() => decodeValues(params as Record<string, string>), [params])
17+
const decodedParams = useMemo(() => decodeValues(params), [params])
1718

18-
return { params: decodedParams, ...rest }
19+
return { params: decodedParams, path }
1920
}
2021

21-
export const decodeValues = (obj: Record<string, string>) => {
22+
export const decodeValues = (obj: Record<string, string | undefined>) => {
2223
const data = Object.keys(obj).reduce((acc, key) => {
2324
acc[key] = obj[key] && decodeURIComponent(obj[key] as string)
2425
return acc
2526

26-
}, {} as Record<string, string>)
27+
}, {} as Record<string, string | undefined>)
2728

2829
return data
2930
}
3031

31-
export const encodeValues = <T extends Record<string, string | string[]>>(obj: T) => {
32+
export const encodeValues = <T extends Record<string, string | string[] | undefined>>(obj: T) => {
3233
const data = Object.entries(removeUndefined(obj))
3334
.reduce((acc, [key, value]) => {
3435

0 commit comments

Comments
 (0)