|
| 1 | +# Prevent creating unstable components inside components (react/no-unstable-nested-components) |
| 2 | + |
| 3 | +Creating components inside components without memoization leads to unstable components. The nested component and all its children are recreated during each re-render. Given stateful children of the nested component will lose their state on each re-render. |
| 4 | + |
| 5 | +React reconcilation performs element type comparison with [reference equality](https://github.com/facebook/react/blob/v16.13.1/packages/react-reconciler/src/ReactChildFiber.js#L407). The reference to the same element changes on each re-render when defining components inside the render block. This leads to complete recreation of the current node and all its children. As a result the virtual DOM has to do extra unnecessary work and [possible bugs are introduced](https://codepen.io/ariperkkio/pen/vYLodLB). |
| 6 | + |
| 7 | +## Rule Details |
| 8 | + |
| 9 | +The following patterns are considered warnings: |
| 10 | + |
| 11 | +```jsx |
| 12 | +function Component() { |
| 13 | + function UnstableNestedComponent() { |
| 14 | + return <div />; |
| 15 | + } |
| 16 | + |
| 17 | + return ( |
| 18 | + <div> |
| 19 | + <UnstableNestedComponent /> |
| 20 | + </div> |
| 21 | + ); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +```jsx |
| 26 | +function SomeComponent({ footer: Footer }) { |
| 27 | + return ( |
| 28 | + <div> |
| 29 | + <Footer /> |
| 30 | + </div> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +function Component() { |
| 35 | + return ( |
| 36 | + <div> |
| 37 | + <SomeComponent footer={() => <div />} /> |
| 38 | + </div> |
| 39 | + ); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```jsx |
| 44 | +class Component extends React.Component { |
| 45 | + render() { |
| 46 | + function UnstableNestedComponent() { |
| 47 | + return <div />; |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <UnstableNestedComponent /> |
| 53 | + </div> |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +The following patterns are **not** considered warnings: |
| 60 | + |
| 61 | +```jsx |
| 62 | +function OutsideDefinedComponent(props) { |
| 63 | + return <div />; |
| 64 | +} |
| 65 | + |
| 66 | +function Component() { |
| 67 | + return ( |
| 68 | + <div> |
| 69 | + <OutsideDefinedComponent /> |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```jsx |
| 76 | +function Component() { |
| 77 | + const MemoizedNestedComponent = React.useCallback(() => <div />, []); |
| 78 | + |
| 79 | + return ( |
| 80 | + <div> |
| 81 | + <MemoizedNestedComponent /> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +```jsx |
| 88 | +function Component() { |
| 89 | + return ( |
| 90 | + <SomeComponent footer={<div />} /> |
| 91 | + ) |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +By default component creation is allowed inside component props only if prop name starts with `render`. See `allowAsProps` option for disabling this limitation completely. |
| 96 | + |
| 97 | +```jsx |
| 98 | +function SomeComponent(props) { |
| 99 | + return <div>{props.renderFooter()}</div>; |
| 100 | +} |
| 101 | + |
| 102 | +function Component() { |
| 103 | + return ( |
| 104 | + <div> |
| 105 | + <SomeComponent renderFooter={() => <div />} /> |
| 106 | + </div> |
| 107 | + ); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Rule Options |
| 112 | + |
| 113 | +```js |
| 114 | +... |
| 115 | +"react/no-unstable-nested-components": [ |
| 116 | + "off" | "warn" | "error", |
| 117 | + { "allowAsProps": true | false } |
| 118 | +] |
| 119 | +... |
| 120 | +``` |
| 121 | + |
| 122 | +You can allow component creation inside component props by setting `allowAsProps` option to true. When using this option make sure you are **calling** the props in the receiving component and not using them as elements. |
| 123 | + |
| 124 | +The following patterns are **not** considered warnings: |
| 125 | + |
| 126 | +```jsx |
| 127 | +function SomeComponent(props) { |
| 128 | + return <div>{props.footer()}</div>; |
| 129 | +} |
| 130 | + |
| 131 | +function Component() { |
| 132 | + return ( |
| 133 | + <div> |
| 134 | + <SomeComponent footer={() => <div />} /> |
| 135 | + </div> |
| 136 | + ); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## When Not To Use It |
| 141 | + |
| 142 | +If you are not interested in preventing bugs related to re-creation of the nested components or do not care about optimization of virtual DOM. |
0 commit comments