Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(suspensive.org): add docs for shouldCatch at ErrorBoundary #1085

Merged
merged 5 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-suns-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": patch
---

fix(react): add jsdoc for shouldCatch prop of ErrorBoundary
221 changes: 221 additions & 0 deletions docs/suspensive.org/src/pages/docs/react/ErrorBoundary.en.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Callout, Sandpack } from '@/components'
import { Tabs } from 'nextra/components'

# ErrorBoundary

Expand Down Expand Up @@ -364,6 +365,226 @@ export const ErrorAfter2s = () => {

</Sandpack>

### props.shouldCatch

shouldCatch determines whether `<ErrorBoundary/>` should catch errors based on conditions.

It accepts three criteria: Boolean, Constructor, and Callback, and defaults to true.

<Tabs items={['Boolean', 'Constructor', 'Callback']}>
<Tabs.Tab>
jgjgill marked this conversation as resolved.
Show resolved Hide resolved

```tsx {10}
jgjgill marked this conversation as resolved.
Show resolved Hide resolved
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { CustomErrorAfter2s } from './CustomErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={false}
fallback={({ error }) => <>{error.message} of Child</>}
>
<CustomErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```
</Tabs.Tab>
<Tabs.Tab>

```tsx {10}
jgjgill marked this conversation as resolved.
Show resolved Hide resolved
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { CustomErrorAfter2s } from './CustomErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={CustomError}
fallback={({ error }) => <>{error.message} of Child</>}
>
<CustomErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```

</Tabs.Tab>
<Tabs.Tab>

```tsx {10}
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { CustomErrorAfter2s } from './CustomErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={(error) => error instanceof CustomError}
fallback={({ error }) => <>{error.message} of Child</>}
>
<CustomErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```
</Tabs.Tab>
</Tabs>

You can also apply multiple criteria through array.

```tsx {10}
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { CustomErrorAfter2s } from './CustomErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={[false, CustomError, (error) => error instanceof CustomError]}
fallback={({ error }) => <>{error.message} of Child</>}
>
<CustomErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```

<Sandpack>

```tsx Example.tsx active
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { CustomErrorAfter2s } from './CustomErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={CustomError}
fallback={({ error }) => <>{error.message} of Child</>}
>
<CustomErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```

```tsx CustomError.ts

export class CustomError extends Error {
constructor(...args: ConstructorParameters<ErrorConstructor>) {
super(...args)
console.error(...args)
}
}
```

```tsx CustomErrorAfter2s.tsx
import { useState, useEffect } from "react";
import { CustomError } from "./CustomError";

export const CustomErrorAfter2s = () => {
const [asyncState, setAsyncState] = useState<
{ isError: true; error: CustomError } | { isError: false; error: null }
>({
isError: false,
error: null
});

useEffect(() => {
setTimeout(() => {
setAsyncState({
isError: true,
error: () => new CustomError("unauthorized")
jgjgill marked this conversation as resolved.
Show resolved Hide resolved
});
}, 2000);
}, []);

if (asyncState.isError) {
throw asyncState.error();
}

return <>No error</>;
};
```

jgjgill marked this conversation as resolved.
Show resolved Hide resolved
</Sandpack>

<br />

<Sandpack>

```tsx Example.tsx active
import { ErrorBoundary } from '@suspensive/react'
import { createElement } from 'react'
import { CustomError } from './CustomError'
import { ErrorAfter2s } from './ErrorAfter2s'

export const Example = () => {
return (
<ErrorBoundary fallback={({ error }) => <>{error.message} of Parent</>}>
<ErrorBoundary
shouldCatch={CustomError}
fallback={({ error }) => <>{error.message} of Child</>}
>
<ErrorAfter2s />
</ErrorBoundary>
</ErrorBoundary>
)
}
```

```tsx CustomError.ts

export class CustomError extends Error {
constructor(...args: ConstructorParameters<ErrorConstructor>) {
super(...args)
console.error(...args)
}
}
```

```tsx /ErrorAfter2s.tsx
import { useState, useEffect } from 'react'

export const ErrorAfter2s = () => {
const [asyncState, setAsyncState] = useState<{ isError: true; error: Error } | { isError: false; error: null }>({
isError: false,
error: null,
})

useEffect(() => {
setTimeout(() => {
setAsyncState({ isError: true, error: { status: 401, message: 'unauthorized' } })
jgjgill marked this conversation as resolved.
Show resolved Hide resolved
}, 2000)
}, [])

if (asyncState.isError) {
throw asyncState.error
}

return <>No error</>
}
```

</Sandpack>

## useErrorBoundary

### useErrorBoundary().setError
Expand Down
Loading
Loading