| 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 | function wrapWithHoc(Component: () => any, index: number) { |
| 14 | function HOC() { |
| 15 | return <Component />; |
| 16 | } |
| 17 | |
| 18 | const displayName = (Component as any).displayName || Component.name; |
| 19 | |
| 20 | HOC.displayName = `withHoc${index}(${displayName})`; |
| 21 | return HOC; |
| 22 | } |
| 23 | |
| 24 | function wrapWithNested(Component: () => any, times: number) { |
| 25 | for (let i = 0; i < times; i++) { |
| 26 | Component = wrapWithHoc(Component, i); |
| 27 | } |
| 28 | |
| 29 | return Component; |
| 30 | } |
| 31 | |
| 32 | function Nested() { |
| 33 | return <div>Deeply nested div</div>; |
| 34 | } |
| 35 | |
| 36 | const DeeplyNested = wrapWithNested(Nested, 100); |
| 37 | |
| 38 | export default function DeeplyNestedComponents(): React.Node { |
| 39 | return ( |
| 40 | <Fragment> |
| 41 | <h1>Deeply nested component</h1> |
| 42 | <DeeplyNested /> |
| 43 | </Fragment> |
| 44 | ); |
| 45 | } |