React
Use function declaration syntax for components and other functions (vs function expression)
Use function declaration/signature line exporting (vs bottom of file)
Only include one exported component per file.
Use interface Props {} for typing main component props (vs named props or
type
) [1]
Every component should exist within a "component file" of the same name & casing.
If local components, helper functions, etc. are extracted into their own files, then a "component folder" should be created and everything should be placed within it (including the component file)
For conditional rendering, prefer multiple returns (early returns) (vs single return with internal conditional rendering of JSX) [1]
Multiple returns (early returns)
✅
if
conditions
Single return
❌
&&
(logical AND operator)❌
? :
(ternary operator)
For event handlers:
use inline anonymous functions (arrow syntax) when the handling logic is minimal and straightforward (vs creating an extracted/named event handler function) [1]
use handleX syntax for extracted/named event handlers functions [1]
X should be consistent with the prop name when possible (onClick prop = handleClick event handler)
use onX syntax for event handler props (component prop names that receive event handler functions) [1]
use e when referring to event objects in event handler functions (vs event, etc.) [1]
For (state) updater functions, use prev or first letters of corresponding state variable as the argument name [1]
If using reducers: [1]
use a switch statement (vs if/else)
use
{
and}
for each case when needing to declare local variables
use more generic action strings (vs state-specific names)
Last updated