Naming React Callback Props and Handlers¶
In React development, consistent naming of callback props and their internal handler functions improves clarity, reduces mental overhead, and makes components easier to understand and maintain. Two common naming patterns are:
onX
: for props passed to a component (e.g.,onClick
,onSubmit
)handleX
: for internal handler functions that implement the logic (e.g.,handleClick
,handleSubmit
)
Following this convention helps developers immediately distinguish event sources from event handlers and navigate through the code more intuitively.
Benefits¶
-
Improved Consistency
: Using consistent naming for callbacks and handlers makes the codebase predictable and easier to follow. -
Clear Ownership
:onX
indicates a function passed into a component (usually from a parent), whilehandleX
clearly marks it as a function defined inside the component. -
Better Autocomplete and IDE Support
: When naming is consistent, it's easier to find and auto-complete handlers in modern editors. -
Easier Refactoring
: You know exactly where to look when refactoring logic or passing down new handlers to children.
Examples¶
Good Example¶
onClick
: prop expected by theButton
componenthandleClick
: local handler inParentComponent
Bad Example¶
This version is less clear:
clickHandler
could be either a prop or a local functionclicked
lacks verb clarity and doesn’t indicate its purpose or trigger
Naming Guidelines¶
Context | Naming Convention |
---|---|
Prop (callback) | onX |
Local handler | handleX |
Event object | event , e |
Examples:
onFormSubmit
→handleFormSubmit
onItemSelect
→handleItemSelect
onMouseEnter
→handleMouseEnter
Considerations¶
- Use verbs in your function names (
handleSubmit
, notsubmissionHandler
) - Avoid inconsistent or ambiguous names like
clickedFn
,myHandler
, orcb
- Stick to camelCase for handlers, even in large codebases