Component Rules
All rules regarding react components
Rules that govern how React component files are written. Every rule is enabled by the
recommended config and applies to .jsx / .tsx files inside a /components/
folder of your project.
component-naming-convention
Require component default export names to start with an uppercase letter and match the file name.
Do
export default function ProfileCard() {
return <section>Profile</section>
}Don't
export default function profileCard() {
return <section>Profile</section>
}The rule flags two distinct problems:
- The default export name does not start with an uppercase letter.
- The default export name does not match the file name (excluding extension).
Do
export default function MatchName() {
return <section>Match</section>
}Don't
export default function DifferentName() {
return <section>Mismatch</section>
}component-one-per-file
Require component files to contain only one component.
Do
export default function MainWidget() {
return <div>Main</div>
}Don't
function MainWidget() {
return <div>Main</div>
}
function SecondaryWidget() {
return <div>Secondary</div>
}
export default MainWidgetThe rule looks for any function declaration or const initialized with an
arrow function / function expression whose name starts with an uppercase
letter. Each additional uppercase component declaration after the first is
reported.
If you have a second component in the same file, move it to its own file under
src/components/.
component-require-default-export
Require component files to have a default export.
Do
export default function UserBadge() {
return <span>Badge</span>
}Don't
export function UserBadge() {
return <span>Badge</span>
}Components in /components/ must be default-exported so they can be imported with a
clean, locally-controlled name and so tree-shaking and lazy loading behave
predictably.
component-default-export-function-declaration
Require default exports in component files to be function declarations.
Do
function OrderSummary() {
return <div>Summary</div>
}
export default OrderSummaryDon't
const ArrowDefault = () => {
return <div>Arrow</div>
}
export default ArrowDefaultInline default-exported arrow functions are also rejected:
// No component name attached to the default export
export default () => {
return <div>Inline</div>
}Function declarations are hoisted, have an explicit name (which is great for stack
traces and React DevTools), and are the convention the rest of this rule pack
relies on — for example, component-props-interface looks up the component by its
declaration name.
component-props-interface
Require component files to define a local, non-exported interface Props at the top
of the file.
Do
interface Props {
name: string
}
export default function GreetingCard({ name }: Props) {
return <h1>{name}</h1>
}Don't
export interface Props {
label: string
}
export default function ExportedPropsExample({ label }: Props) {
return <p>{label}</p>
}The rule enforces all of the following at once:
- A
Propsinterface must be declared locally in the file. - It must be declared with
interface, not atypealias. - It must not be exported.
- It must be declared before the component's default export.
- It must be named exactly
Props— no other interface declarations are allowed in component files.
// Use `interface Props`, not `type Props`
type Props = {
value: number
}
export default function AliasPropsExample({ value }: Props) {
return <p>{value}</p>
}If your component needs shared prop types, import them from a /types/ folder
rather than re-declaring or exporting the Props interface from the component
file. The rule treats an imported type used as the props annotation as a valid
exception to the "must declare Props" check.