-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): prevent rendering fallback if not shouldCatch error (#1410)
Co-authored-by: lucas0530 <23375098+lucas0530@users.noreply.github.com> Co-authored-by: HYUNGU KANG <69349293+linegu@users.noreply.github.com>
- Loading branch information
1 parent
eb2423b
commit ce3cb11
Showing
8 changed files
with
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@suspensive/react": patch | ||
--- | ||
|
||
fix(react): prevent rendering fallback if not shouldCatch error |
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
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