From 928be8e980ac4e396e84b4c587e978ef651bfef4 Mon Sep 17 00:00:00 2001 From: mathiassoeholm Date: Wed, 24 Apr 2019 10:13:38 +0200 Subject: [PATCH 1/4] Updated error boundary example with working code --- README.md | 27 +++++++++++++++---- .../src/hoc/with-error-boundary.usage.tsx | 27 +++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 22aa056..29ca890 100644 --- a/README.md +++ b/README.md @@ -624,6 +624,7 @@ export const withErrorBoundary = ( ```tsx import * as React from 'react'; +import {useState} from 'react'; import { withErrorBoundary } from '../hoc'; import { ErrorMessage } from '../components'; @@ -631,11 +632,27 @@ import { ErrorMessage } from '../components'; const ErrorMessageWithErrorBoundary = withErrorBoundary(ErrorMessage); -const BrokenButton = () => ( - -); +const BrokenButton = () => { + const [throwError, setThrowError] = useState(false); + + if (throwError) { + throw new Error('Catch me!'); + } + + return ( + + ); +}; export default () => ( diff --git a/playground/src/hoc/with-error-boundary.usage.tsx b/playground/src/hoc/with-error-boundary.usage.tsx index 3045b75..06dd4c7 100644 --- a/playground/src/hoc/with-error-boundary.usage.tsx +++ b/playground/src/hoc/with-error-boundary.usage.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import {useState} from 'react'; import { withErrorBoundary } from '../hoc'; import { ErrorMessage } from '../components'; @@ -6,11 +7,27 @@ import { ErrorMessage } from '../components'; const ErrorMessageWithErrorBoundary = withErrorBoundary(ErrorMessage); -const BrokenButton = () => ( - -); +const BrokenButton = () => { + const [throwError, setThrowError] = useState(false); + + if (throwError) { + throw new Error('Catch me!'); + } + + return ( + + ); +}; export default () => ( From 81840f7296ffacb634ff173ce36a602b4778e903 Mon Sep 17 00:00:00 2001 From: Mathias Soeholm Date: Sun, 28 Apr 2019 22:03:45 +0200 Subject: [PATCH 2/4] Updates based on review --- .../src/hoc/with-error-boundary.usage.tsx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/playground/src/hoc/with-error-boundary.usage.tsx b/playground/src/hoc/with-error-boundary.usage.tsx index 06dd4c7..772fa50 100644 --- a/playground/src/hoc/with-error-boundary.usage.tsx +++ b/playground/src/hoc/with-error-boundary.usage.tsx @@ -1,5 +1,4 @@ -import * as React from 'react'; -import {useState} from 'react'; +import React, {useState} from 'react'; import { withErrorBoundary } from '../hoc'; import { ErrorMessage } from '../components'; @@ -7,21 +6,23 @@ import { ErrorMessage } from '../components'; const ErrorMessageWithErrorBoundary = withErrorBoundary(ErrorMessage); +const BrokenComponent = () => { + throw new Error('I\'m broken! Don\'t render me.'); +}; + const BrokenButton = () => { - const [throwError, setThrowError] = useState(false); + const [shouldRenderBrokenComponent, setShouldRenderBrokenComponent] = + useState(false); - if (throwError) { - throw new Error('Catch me!'); + if (shouldRenderBrokenComponent) { + return ; } return (