๐Ÿ› /Dev Tool

[ESLint] react/no-unstable-nested-components

ํ•œ๋‚˜ 2022. 1. 31. 20:38

 

 

 

 

์•„๋ž˜ ๊ฐ™์€ ํŒจํ„ด์˜ ์ฝ”๋“œ๋Š” no-unstable-nested-components ์—๋Ÿฌ๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋œ๋‹ค.

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.
function Component() {
    function UnstableNestedCOmponent() {
        return <div />
    }

    return (
        <div>
              <UnstableNestedComponent />
        </div>
    )
}

 

์ด๋Ÿฐ ํŒจํ„ด์€ ์œ„ ์‚ฌ๋ก€๋ณด๋‹ค ๋” ๋งŽ๋‹ค. ์•„๋ž˜์˜ ๊ฒฝ์šฐ๋Š” ๊ฒฌ๊ณ ํ•œ ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. 

 

function OutsideDefinedComponent(props) {
    return <div />;
}

function Component() {
    return (
           <div>
              <OutsideDefinedComponent />
          </div>
    )
}
function Component() {
    const MemoizedNestedComponent = React.useCallback(() => <div />, []);
      return (
        <div>
              <MemoizedNestedComponent />
          </div>
    )
}
function Component() {
    return (
        <SomeComponent footer={<div />} />
    )
}

 

์ด๋Ÿฐ ๋ถˆ์•ˆ์ •ํ•œ ์ปดํฌ๋„ŒํŠธ์˜ ํŒจํ„ด์„ ๋ง‰์œผ๋ ค๋ฉด .eslintrc ํŒŒ์ผ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์„ค์ •ํ•ด์ค€๋‹ค. 

...
"react/no-unstable-nested-components": [
    "error",
  { "allowAsProps" : false }
]