iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🖱️

Event Handler Naming: onClick or handleClick?

に公開

Introduction

When developing web applications using JavaScript or its frameworks, do you ever struggle with naming conventions for event handlers? I have often wondered whether to use names like onClick, handleClick, onSubmit, or handleSubmit.

The official React documentation includes guidance on how to name event handlers, so I would like to share it with you.

Conclusion: Event handler names should be handle[Event]

In the React official documentation, it is stated as follows:

By convention, it is common to name event handlers as handle followed by the event name. onClick={handleClick}, onMouseEnter={handleMouseEnter}, and so on are commonly seen.

As described, naming event handlers as handle[Event] is the common practice. Following this convention makes it easier for other developers to infer what kind of event a handler is processing when reading the code.

Summary

Event handler names should be handle[Event].

Discussion