์๋ ๊ฐ์ ํจํด์ ์ฝ๋๋ 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 }
]