| description | Use when writing, editing, or reviewing React components. Covers component structure, useState, useEffect, props declaration, children, and JSX patterns. |
|---|---|
| applyTo | **/*.tsx |
- Name setter with
set+ same name as state variable:const [isOpen, setIsOpen] = useState(false). - Never:
const [isOpen, openModal] = useState(false).
- Use one
useEffectper concern. Never combine unrelated side effects into a single hook. - Place all
useEffectcalls at the bottom of the component body, just beforereturn. Exception: a render-function that depends on complex conditional logic may appear afteruseEffect, directly beforereturn.
- Do not assign JSX to variables before
return. Write expressions directly inside thereturnstatement. Exception: extract to a variable only when the condition is deeply nested or the inline expression significantly hurts readability. In that case, use a named function (not a variable) and call it insidereturn. - When rendering lists, always provide an explicit
keyprop. Never use array index askeyfor dynamic/reorderable lists.
- Never declare constants, lookup objects, or pure helper functions inside the component body. Move them outside the component, into a
constants.ts,helpers.ts, orutils.tsfile co-located with the component, or tosrc/shared/(place depends on project structure) if reused elsewhere.
- Declare the
Propstype in the same file, directly above the component function. - Name it exactly
Props(notMyComponentProps). - Do not export
Propsunless it is consumed by another module. If you must export it, rename on export:export { type Props as MyComponentProps }. - Optional props must have default values.
- Required component/React-node props
- Optional component/React-node props
- Required value props
- Optional value props
- Required function props
- Optional function props
- If
childrenis required, declare it explicitly inPropsas required. - If
childrenis optional, usePropsWithChildren<Props>from React.
- Name event handler props with
onprefix:onClick,onSubmit,onInputChange. - Use method syntax in the type:
onClick(): voidrather thanonClick: () => void.
- Use semantic HTML elements in JSX:
<header>,<nav>,<main>,<section>,<article>,<footer>,<button>,<form>, etc. Do not use<div>or<span>where a semantic element exists. - Placeholder links (no real URL yet) must use
href="#mock-address". - Links to external sites must have
target="_blank". - Every link with
target="_blank"must also haverel="noreferrer"to prevent the opened page from accessingwindow.openerand to avoid sending the referrer header.
- Use named exports for components. Avoid default exports.
- Every folder with multiple component files must have an
index.tsthat re-exports the public API.