Skip to content

Error boundary example fix #166

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,35 @@ export const withErrorBoundary = <BaseProps extends InjectedProps>(

```tsx
import * as React from 'react';
import {useState} from 'react';

import { withErrorBoundary } from '../hoc';
import { ErrorMessage } from '../components';

const ErrorMessageWithErrorBoundary =
withErrorBoundary(ErrorMessage);

const BrokenButton = () => (
<button type="button" onClick={() => { throw new Error(`Catch me!`); }}>
{`Throw nasty error`}
</button >
);
const BrokenButton = () => {
const [throwError, setThrowError] = useState(false);

if (throwError) {
throw new Error('Catch me!');
}

return (
<button
type="button"
onClick={() => {
// We don't just throw the error here, because error boundaries
// only catch errors thrown by render and lifecycle methods. Read more:
// https://reactjs.org/docs/error-boundaries.html#how-about-event-handlers
setThrowError(true);
}}
>
{`Throw nasty error`}
</button>
);
};

export default () => (
<ErrorMessageWithErrorBoundary>
Expand Down
27 changes: 22 additions & 5 deletions playground/src/hoc/with-error-boundary.usage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import * as React from 'react';
import {useState} from 'react';

import { withErrorBoundary } from '../hoc';
import { ErrorMessage } from '../components';

const ErrorMessageWithErrorBoundary =
withErrorBoundary(ErrorMessage);

const BrokenButton = () => (
<button type="button" onClick={() => { throw new Error(`Catch me!`); }}>
{`Throw nasty error`}
</button >
);
const BrokenButton = () => {
const [throwError, setThrowError] = useState(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename throwError to shouldRenderBrokenComponent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line became quite long because of [shouldRenderBrokenComponent, setShouldRenderBrokenComponent]
so I think setShouldBreak would be a better name.


if (throwError) {
throw new Error('Catch me!');
}

return (
<button
type="button"
onClick={() => {
// We don't just throw the error here, because error boundaries
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer just:

Error boundaries catch errors during rendering and do not work inside event handlers:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I think that comment will just confuse whoever is reading through the example. I removed the comment altogether now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fine 👍

// only catch errors thrown by render and lifecycle methods. Read more:
// https://reactjs.org/docs/error-boundaries.html#how-about-event-handlers
setThrowError(true);
}}
>
{`Throw nasty error`}
</button>
);
};

export default () => (
<ErrorMessageWithErrorBoundary>
Expand Down