React

react/no-nested-component

Components are declared at module level, never inside another component's body.

poważnaręcznefe-react

Dlaczego

A component defined inside another is a new function on every render, so React sees a different component type each time: it unmounts and remounts it, losing its state, its input focus and its DOM, and it costs a full re-render of that subtree.

Poprawka

Move the inner component to the top level of the module and pass what it needs as props. For a small piece of markup that needs the outer scope, render it inline or with a plain function call that returns JSX, not as <Inner />.

Przykłady

// bad
function List({ items }) {
  function Row({ item }) { return <li>{item.name}</li> }
  return <ul>{items.map((i) => <Row key={i.id} item={i} />)}</ul>
}
// good
function Row({ item }) { return <li>{item.name}</li> }
function List({ items }) {
  return <ul>{items.map((i) => <Row key={i.id} item={i} />)}</ul>
}

Źródła

Fakty

Identyfikator
react/no-nested-component
Waga
poważna
Sprawdzanie
ręczne ocenia człowiek; raport podaje to jako listę kontrolną
Dotyczy plików
jsx
Tylko w projektach z
react next
Uczy jej skill
fe-react React components
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain react/no-nested-component