React
Last updated
Last updated
Always use JSX syntax (vs React.createElement)
Always use React functional components (vs class components)
Use named exports for components and other functions/variables/etc. (vs default exports)
Only include one exported component per file.
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)
Multiple returns (early returns)
✅ if
conditions
Single return
❌ &&
(logical AND operator)
❌ ? :
(ternary operator)
For event handlers:
X should be consistent with the prop name when possible (onClick prop = handleClick event handler)
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)
For conditional rendering, prefer multiple returns (early returns) (vs single return with internal conditional rendering of JSX)
use inline anonymous functions (arrow syntax) when the handling logic is minimal and straightforward (vs creating an extracted/named event handler function)
use handleX syntax for extracted/named event handlers functions
use onX syntax for event handler props (component prop names that receive event handler functions)
use e when referring to event objects in event handler functions (vs event, etc.)
If there is only a single instance of a prop name in a given component (which can often happen, with modular, components), use it for the function name
If there are multiple instances of a prop name, then follow the convention (append further specificity to the end of the name), or use more specific/descriptive
For (state) updater functions, use prev or first letters of corresponding state variable as the argument name
If using reducers:
If using Immer, use updateX syntax for the updater function (vs setX), and draft