Skip to content

Commit d1ed86c

Browse files
icd2k3jschrader-nr
andauthored
refactor: switch from react-router peer dep to react-router-dom (#64)
* refactor: switch from react-router peer dep to react-router-dom * chore: update yarn lockfile * fix: some TS warnings in test file * fix: types Co-authored-by: Justin Schrader <[email protected]>
1 parent 0f21368 commit d1ed86c

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-react-router-breadcrumbs",
3-
"version": "3.2.1",
3+
"version": "4.0.0",
44
"description": "A hook for displaying and setting breadcrumbs for react router",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",
@@ -11,7 +11,7 @@
1111
"types": "dist/index.d.ts",
1212
"peerDependencies": {
1313
"react": ">=16.8",
14-
"react-router": ">=6.0.0"
14+
"react-router-dom": ">=6.0.0"
1515
},
1616
"scripts": {
1717
"prepublishOnly": "yarn build && pinst --disable",
@@ -55,7 +55,7 @@
5555
"pinst": "^3.0.0",
5656
"react": "^18.2.0",
5757
"react-dom": "^18.2.0",
58-
"react-router": "^6.3.0",
58+
"react-router-dom": "^6.3.0",
5959
"rollup": "^2.79.0",
6060
"rollup-plugin-size": "^0.2.2",
6161
"rollup-plugin-terser": "^7.0.2",

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const formats = [
2727

2828
const globals = {
2929
react: 'React',
30-
'react-router': 'ReactRouter',
30+
'react-router-dom': 'ReactRouterDom',
3131
};
3232

3333
export default formats.map(({ plugins, file, format }) => ({

src/index.test.tsx

+20-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import '@testing-library/jest-dom';
44
import React from 'react';
55
import { render, screen } from '@testing-library/react';
6-
import { MemoryRouter as Router, useLocation } from 'react-router';
7-
import useBreadcrumbs, { getBreadcrumbs, createRoutesFromChildren, BreadcrumbsRoute, Route, Options } from './index';
6+
import { MemoryRouter as Router, useLocation } from 'react-router-dom';
7+
import useBreadcrumbs, { getBreadcrumbs, createRoutesFromChildren, BreadcrumbsRoute, Route, Options, BreadcrumbComponentProps } from './index';
88

99
// imports to test compiled builds
1010
import useBreadcrumbsCompiledES, {
@@ -19,6 +19,11 @@ import useBreadcrumbsCompiledCJS, {
1919
getBreadcrumbs as getBreadcrumbsCompiledCJS,
2020
} from '../dist/cjs/index';
2121

22+
interface ExtraPropsTest extends BreadcrumbComponentProps {
23+
foo?: string;
24+
bar?: string;
25+
}
26+
2227
const components = {
2328
Breadcrumbs: ({
2429
useBreadcrumbs: useBreadcrumbsHook,
@@ -69,23 +74,23 @@ const components = {
6974

7075
BreadcrumbLocationTest: ({
7176
location,
72-
}: {
73-
location?: {
74-
state?: { isLocationTest?: boolean }
75-
}
76-
}) => <span>{location?.state?.isLocationTest ? 'pass' : 'fail'}</span>,
77+
}: BreadcrumbComponentProps) => (
78+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
79+
// @ts-ignore
80+
<span>{location?.state?.isLocationTest ? 'pass' : 'fail'}</span>
81+
),
7782

78-
BreadcrumbExtraPropsTest: ({ foo, bar }: { foo: string, bar: string }) => (
79-
<span>
80-
{foo}
81-
{bar}
82-
</span>
83+
BreadcrumbExtraPropsTest: (props: ExtraPropsTest) => (
84+
<>
85+
<span>{props.foo}</span>
86+
<span>{props.bar}</span>
87+
</>
8388
),
8489

8590
BreadcrumbMemoized: React.memo(() => <span>Memoized</span>),
8691

8792
// eslint-disable-next-line react/prefer-stateless-function
88-
BreadcrumbClass: class BreadcrumbClass extends React.PureComponent {
93+
BreadcrumbClass: class BreadcrumbClass extends React.PureComponent<BreadcrumbComponentProps> {
8994
render() {
9095
return <span>Class</span>;
9196
}
@@ -457,6 +462,8 @@ describe('use-react-router-breadcrumbs', () => {
457462
},
458463
];
459464

465+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
466+
// @ts-ignore arbitrary prop is not expected, but should not error
460467
renderer({ pathname: '/one', routes });
461468
expect(getByTextContent('Home / foobar')).toBeTruthy();
462469
});

src/index.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@ import {
2323
useLocation,
2424
RouteObject,
2525
Params,
26-
PathPattern,
2726
Route,
2827
PathRouteProps,
2928
LayoutRouteProps,
3029
IndexRouteProps,
31-
} from 'react-router';
30+
} from 'react-router-dom';
3231

3332
type Location = ReturnType<typeof useLocation>;
3433

34+
/**
35+
* This type interface is copied in directly from react-router (react-router-dom does not export it)
36+
*/
37+
interface PathPattern<Path extends string = string> {
38+
path: Path;
39+
caseSensitive?: boolean;
40+
end?: boolean;
41+
}
42+
3543
export interface Options {
3644
disableDefaults?: boolean;
3745
excludePaths?: string[];
@@ -61,6 +69,7 @@ export interface BreadcrumbComponentProps<ParamKey extends string = string> {
6169
key: string;
6270
match: BreadcrumbMatch<ParamKey>;
6371
location: Location;
72+
[x: string]: unknown;
6473
}
6574

6675
export type BreadcrumbComponentType<ParamKey extends string = string> =

yarn.lock

+16-3
Original file line numberDiff line numberDiff line change
@@ -6897,7 +6897,20 @@ __metadata:
68976897
languageName: node
68986898
linkType: hard
68996899

6900-
"react-router@npm:^6.3.0":
6900+
"react-router-dom@npm:^6.3.0":
6901+
version: 6.3.0
6902+
resolution: "react-router-dom@npm:6.3.0"
6903+
dependencies:
6904+
history: ^5.2.0
6905+
react-router: 6.3.0
6906+
peerDependencies:
6907+
react: ">=16.8"
6908+
react-dom: ">=16.8"
6909+
checksum: 77603a654f8a8dc7f65535a2e5917a65f8d9ffcb06546d28dd297e52adcc4b8a84377e0115f48dca330b080af2da3e78f29d590c89307094d36927d2b1751ec3
6910+
languageName: node
6911+
linkType: hard
6912+
6913+
"react-router@npm:6.3.0":
69016914
version: 6.3.0
69026915
resolution: "react-router@npm:6.3.0"
69036916
dependencies:
@@ -8116,14 +8129,14 @@ __metadata:
81168129
pinst: ^3.0.0
81178130
react: ^18.2.0
81188131
react-dom: ^18.2.0
8119-
react-router: ^6.3.0
8132+
react-router-dom: ^6.3.0
81208133
rollup: ^2.79.0
81218134
rollup-plugin-size: ^0.2.2
81228135
rollup-plugin-terser: ^7.0.2
81238136
typescript: ^4.8.3
81248137
peerDependencies:
81258138
react: ">=16.8"
8126-
react-router: ">=6.0.0"
8139+
react-router-dom: ">=6.0.0"
81278140
languageName: unknown
81288141
linkType: soft
81298142

0 commit comments

Comments
 (0)