DEV Community

nedajahanfar
nedajahanfar

Posted on

1

Event Handler Behavior in React vs HTML

In HTML , when you write this :
<button onclick="handleClick()">Click me</button>

The browser sees the string "handleClick()" and stores it as text inside the element.

It does not run it immediately — it waits for the user to click.

When the click happens, the browser reads the string and executes it like code.
So:

** The function call is stored as a string, not as an actual JavaScript command.

It’s only run when the event occurs.
**

In React when you write this :
<button onClick={handleClick}>Click me</button>

**React sees handleClick as a real function reference, not a string.

You are telling React:

“When this button is clicked, call this function.”
**

If you write :

onClick={handleClick()}

You're calling the function immediately during render .
Because the () makes it run now, not later.

In HTML, event handlers are written as strings that are stored inside the element and only run when the event happens.
In React, event handlers are real functions that React keeps track of — so you don’t use () because you don’t want the function to run right away.
If you do need to pass arguments (which requires ()), you wrap the call inside an arrow function so it waits for the event to trigger.

onClick={() => handleClick("hi")}

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more