-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
fix(react): prevent rendering fallback if not shouldCatch error
Co-authored-by: lucas0530 <[email protected]> Co-authored-by: HYUNGU KANG <[email protected]>
1 parent
07b6b77
commit 5501676
Showing
6 changed files
with
60 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
examples/visualization/src/app/react/ErrorBoundary/shouldCatch/renderPhase/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use client' | ||
|
||
import { ErrorBoundary } from '@suspensive/react' | ||
import { Throw } from '~/components/Throw' | ||
|
||
export default function page() { | ||
return ( | ||
<ErrorBoundary fallback={() => <>root fallback</>}> | ||
<ErrorBoundary | ||
shouldCatch={(error) => error.message !== 'children error message'} | ||
fallback={() => { | ||
console.log("child ErrorBoundary's fallback") | ||
return <>child ErrorBoundary's fallback</> | ||
}} | ||
> | ||
<Throw.Error message="children error message" after={2000}> | ||
before throw Error | ||
</Throw.Error> | ||
</ErrorBoundary> | ||
</ErrorBoundary> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { type PropsWithChildren, useState } from 'react' | ||
import { useTimeout } from '~/hooks' | ||
|
||
export const Throw = { | ||
Error: ({ message, after = 0, children }: PropsWithChildren<{ message: string; after?: number }>) => { | ||
const [isNeedThrow, setIsNeedThrow] = useState(after === 0) | ||
if (isNeedThrow) { | ||
throw new Error(message) | ||
} | ||
useTimeout(() => setIsNeedThrow(true), after) | ||
return <>{children}</> | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useTimeout } from './useTimeout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useCallback, useEffect, useRef } from 'react' | ||
|
||
export const useTimeout = (fn: () => void, ms: number) => { | ||
const fnRef = useRef(fn) | ||
fnRef.current = fn | ||
const fnPreserved = useCallback(() => fnRef.current(), []) | ||
useEffect(() => { | ||
const id = setTimeout(fnPreserved, ms) | ||
return () => clearTimeout(id) | ||
}, [fnPreserved, ms]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters