1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import * as React from 'react';
    
  11. import {Fragment} from 'react';
    
  12. 
    
  13. class ErrorBoundary extends React.Component {
    
  14.   state: {hasError: boolean} = {hasError: false};
    
  15. 
    
  16.   static getDerivedStateFromError(error: any): {hasError: boolean} {
    
  17.     return {hasError: true};
    
  18.   }
    
  19. 
    
  20.   render(): any {
    
  21.     const {hasError} = this.state;
    
  22.     if (hasError) {
    
  23.       return (
    
  24.         <div
    
  25.           style={{
    
  26.             color: 'red',
    
  27.             border: '1px solid red',
    
  28.             borderRadius: '0.25rem',
    
  29.             margin: '0.5rem',
    
  30.             padding: '0.5rem',
    
  31.           }}>
    
  32.           An error was thrown.
    
  33.         </div>
    
  34.       );
    
  35.     }
    
  36. 
    
  37.     const {children} = this.props;
    
  38.     return (
    
  39.       <div
    
  40.         style={{
    
  41.           border: '1px solid gray',
    
  42.           borderRadius: '0.25rem',
    
  43.           margin: '0.5rem',
    
  44.           padding: '0.5rem',
    
  45.         }}>
    
  46.         {children}
    
  47.       </div>
    
  48.     );
    
  49.   }
    
  50. }
    
  51. 
    
  52. // $FlowFixMe[missing-local-annot]
    
  53. function Component({label}) {
    
  54.   return <div>{label}</div>;
    
  55. }
    
  56. 
    
  57. export default function ErrorBoundaries(): React.Node {
    
  58.   return (
    
  59.     <Fragment>
    
  60.       <h1>Nested error boundaries demo</h1>
    
  61.       <ErrorBoundary>
    
  62.         <Component label="Outer component" />
    
  63.         <ErrorBoundary>
    
  64.           <Component label="Inner component" />
    
  65.         </ErrorBoundary>
    
  66.       </ErrorBoundary>
    
  67.       <ErrorBoundary>
    
  68.         <Component label="Neighbour component" />
    
  69.       </ErrorBoundary>
    
  70.     </Fragment>
    
  71.   );
    
  72. }