File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed
packages/react-router-dom-v5-compat Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 21
21
- bobziroll
22
22
- BrianT1414
23
23
- brockross
24
+ - brookslybrand
24
25
- brophdawg11
25
26
- btav
26
27
- bvangraafeiland
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @jest -environment node
3
+ */
4
+ import * as React from "react" ;
5
+ import * as ReactDOMServer from "react-dom/server" ;
6
+ import { act } from "react-dom/test-utils" ;
7
+ import { CompatRouter , Routes } from "../index" ;
8
+
9
+ // Have to mock react-router-dom to have a comparable API to v5, otherwise it will
10
+ // be using v6's API and fail
11
+ jest . mock ( "react-router-dom" , ( ) => ( {
12
+ useHistory : ( ) => ( { location : "/" } ) ,
13
+ } ) ) ;
14
+
15
+ describe ( "CompatRouter" , ( ) => {
16
+ it ( "should not warn about useLayoutEffect when server side rendering" , ( ) => {
17
+ const consoleErrorSpy = jest . spyOn ( console , "error" ) ;
18
+
19
+ act ( ( ) => {
20
+ ReactDOMServer . renderToStaticMarkup (
21
+ < CompatRouter >
22
+ < Routes />
23
+ </ CompatRouter >
24
+ ) ;
25
+ } ) ;
26
+
27
+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 0 ) ;
28
+ consoleErrorSpy . mockRestore ( ) ;
29
+ } ) ;
30
+ } ) ;
Original file line number Diff line number Diff line change @@ -23,14 +23,25 @@ export function CompatRoute(props: any) {
23
23
) ;
24
24
}
25
25
26
+ // Copied with 💜 from https://github.com/bvaughn/react-resizable-panels/blob/main/packages/react-resizable-panels/src/hooks/useIsomorphicEffect.ts
27
+ const canUseEffectHooks = ! ! (
28
+ typeof window !== "undefined" &&
29
+ typeof window . document !== "undefined" &&
30
+ typeof window . document . createElement !== "undefined"
31
+ ) ;
32
+
33
+ const useIsomorphicLayoutEffect = canUseEffectHooks
34
+ ? React . useLayoutEffect
35
+ : ( ) => { } ;
36
+
26
37
export function CompatRouter ( { children } : { children : React . ReactNode } ) {
27
38
let history = useHistory ( ) ;
28
39
let [ state , setState ] = React . useState ( ( ) => ( {
29
40
location : history . location ,
30
41
action : history . action ,
31
42
} ) ) ;
32
43
33
- React . useLayoutEffect ( ( ) => {
44
+ useIsomorphicLayoutEffect ( ( ) => {
34
45
history . listen ( ( location : Location , action : Action ) =>
35
46
setState ( { location, action } )
36
47
) ;
You can’t perform that action at this time.
0 commit comments