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 all 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
2 changes: 0 additions & 2 deletions configs/tsup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Options } from 'tsup'

export const options: Options = {
clean: true,
banner: { js: '"use client"' },
format: ['cjs', 'esm'],
target: ['chrome51', 'firefox53', 'edge18', 'safari11', 'ios11', 'opera38', 'es6', 'node14'],
Expand All @@ -12,7 +11,6 @@ export const options: Options = {
}

export const scriptOptions: Options = {
clean: true,
format: ['cjs'],
target: ['chrome51', 'firefox53', 'edge18', 'safari11', 'ios11', 'opera38', 'es6', 'node14'],
entry: ['src/scripts/*.{ts,tsx}', '!**/*.{spec,test,test-d,bench}.*'],
Expand Down
202 changes: 199 additions & 3 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 @@ -61,7 +62,7 @@ export const ErrorAfter2s = () => {

useEffect(() => {
setTimeout(() => {
setAsyncState({ isError: true, error: { status: 401, message: 'unauthorized' } })
setAsyncState({ isError: true, error: new Error('error made by Error') })
}, 2000)
}, [])

Expand Down Expand Up @@ -196,7 +197,7 @@ export const ErrorAfter2s = () => {

useEffect(() => {
setTimeout(() => {
setAsyncState({ isError: true, error: { status: 401, message: 'unauthorized' } })
setAsyncState({ isError: true, error: new Error('error made by Error') })
}, 2000)
}, [])

Expand Down Expand Up @@ -350,7 +351,202 @@ export const ErrorAfter2s = () => {

useEffect(() => {
setTimeout(() => {
setAsyncState({ isError: true, error: { status: 401, message: 'unauthorized' } })
setAsyncState({ isError: true, error: new Error('error made by Error') })
}, 2000)
}, [])

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

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

</Sandpack>

### props.shouldCatch

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

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

<Tabs items={['Boolean', 'ErrorConstructor', 'Callback']}>
<Tabs.Tab>

```tsx /shouldCatch/
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

</Tabs.Tab>
<Tabs.Tab>

```tsx /shouldCatch/
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

</Tabs.Tab>
<Tabs.Tab>

```tsx /shouldCatch/
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

</Tabs.Tab>
</Tabs>

You can also apply multiple criteria through array.

```tsx /shouldCatch/
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

<Sandpack>

```tsx Example.tsx active
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

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

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("error made by CustomError")
});
}, 2000);
}, []);

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

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

</Sandpack>

<br />

<Sandpack>

```tsx Example.tsx active
import { ErrorBoundary } from '@suspensive/react'
import { useState, useEffect, createElement } from 'react'

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

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

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: new Error('error made by Error') })
}, 2000)
}, [])

Expand Down
Loading
Loading