diff --git a/.changeset/two-weeks-serve.md b/.changeset/two-weeks-serve.md new file mode 100644 index 000000000..12eb91b48 --- /dev/null +++ b/.changeset/two-weeks-serve.md @@ -0,0 +1,5 @@ +--- +"@suspensive/react": minor +--- + +feat(react): add useErrorBoundaryFallbackProps diff --git a/packages/react/src/ErrorBoundary.en.mdx b/packages/react/src/ErrorBoundary.en.mdx index e11dc75aa..b8ec2c519 100644 --- a/packages/react/src/ErrorBoundary.en.mdx +++ b/packages/react/src/ErrorBoundary.en.mdx @@ -48,6 +48,68 @@ const ErrorAfter4s = () => { ![Example banner](/gif/errorboundary-example.gif) +:::tip + +## Define component as ``'s fallback + +### ErrorBoundaryFallbackProps + +If you want to deliver a declared component as ``'s fallback, you can use the `ErrorBoundaryFallbackProps` type to declare the component easily. + +```tsx +import type { ErrorBoundaryFallbackProps } from '@suspensive/react' + +const ErrorBoundaryFallback = ({ reset, error }: ErrorBoundaryFallbackProps) => ( + <> + + {error.message} + +) + +const Example = () => ( + + + +) +``` + +::: + +:::tip + +## Use `` fallback props without prop drilling + +### useErrorBoundaryFallbackProps + +If component using `reset` method and `error` object is nested, prop drilling cannot be avoided. +The `useErrorBoundaryFallbackProps` allows you to access the `reset` method and `error` objects without prop drilling. + +```tsx +import { ErrorBoundary, useErrorBoundaryFallbackProps } from '@suspensive/react' + +const Nested = () => { + const { reset, error } = useErrorBoundaryFallbackProps() + + return ( + <> + + {error.message} + + ) +} + +// There's no need to pass fallback Prop here! +const ErrorBoundaryFallback = () => + +const Example = () => ( + + + +) +``` + +::: + ### props.resetKeys If you want to reset `` by component where is outside of ``'s fallback. Inject any resetKey in resetKeys. resetKeys work only when at least one element of array is changed. you don't need to worry about provide new array as resetKeys like how useEffect's dependency array work. @@ -123,25 +185,9 @@ useErrorBoundary is experimental feature, this interfaces could be changed ::: -We can reset `` without props using useErrorBoundary.reset in ErrorBoundaryFallback. - -```tsx -import { ErrorBoundary, useErrorBoundary } from '@suspensive/react' - -const ErrorBoundaryFallback = () => { - const errorBoundary = useErrorBoundary() - - return + {error.message} + +) + +const Example = () => ( + + + +) +``` + +::: + +:::tip + +## `` fallback props을 prop drilling 없이 사용하기 + +### useErrorBoundaryFallbackProps + +`error` 객체와 `reset` 메소드을 사용하려는 컴포넌트가 중첩되면 prop drilling을 피할 수 없습니다. +이 때, `useErrorBoundaryFallbackProps`을 통해, prop drilling 없이 `reset` 메소드와 `error` 객체에 접근할 수 있습니다. + +```tsx +import { ErrorBoundary, useErrorBoundaryFallbackProps } from '@suspensive/react' + +const Nested = () => { + const { reset, error } = useErrorBoundaryFallbackProps() + + return ( + <> + + {error.message} + + ) +} + +// 여기서 fallbackProp 을 전달할 필요가 없어집니다! +const ErrorBoundaryFallback = () => + +const Example = () => ( + + + +) +``` + +::: + ### props.resetKeys -``의 fallback 외부에 있는 컴포넌트가 ``를 reset하려면 resetKeys배열에 resetKey를 할당하면 됩니다. resetKeys는 배열의 하나 이상의 요소가 변경된 경우에만 작동합니다. useEffect의 종속성 배열이 작동하는 방식과 같이 resetKeys로 매 렌더링마다 새 배열을 주입하는 것을 걱정할 필요도 없습니다. +``의 fallback 외부에 있는 컴포넌트가 ``를 reset하려면 resetKeys 배열에 resetKey를 할당하면 됩니다. resetKeys는 배열의 하나 이상의 요소가 변경된 경우에만 작동합니다. useEffect의 종속성 배열이 작동하는 방식과 같이 resetKeys로 매 렌더링마다 새 배열을 주입하는 것을 걱정할 필요도 없습니다. ```tsx import { ErrorBoundary } from '@suspensive/react' @@ -123,25 +185,9 @@ useErrorBoundary는 실험 기능이므로 이 인터페이스는 변경될 수 ::: -ErrorBoundaryFallback에서 useErrorBoundary.reset을 사용해 props 없이도 ``을 reset할 수 있습니다. - -```tsx -import { ErrorBoundary, useErrorBoundary } from '@suspensive/react' - -const ErrorBoundaryFallback = () => { - const errorBoundary = useErrorBoundary() - - return + return }} > {createElement(function ErrorComponent() {