On this page

09. Difference between onClick in React and onclick in HTML

📄 Noteseasy#events#jsx#dom

Short answer

In HTML, onclick is a lowercase attribute holding a string of code that runs in the global scope, and returning false cancels the default action. In React, onClick is a camelCase prop that takes a function reference, receives a synthetic event, is delegated from the root, and you must call e.preventDefault() explicitly to cancel default behaviour.

Explanation

HTML onclick is like leaving a note that says "do this" in text; the browser parses and evaluates it later. React's onClick is like handing over the actual tool to use.

HTML onclickReact onClick
CasingLowercasecamelCase
ValueString of JS: onclick="save()"Function: onClick={save}
ScopeGlobal (window)Closure: sees props and state
Event objectNative eventSyntheticEvent (e.nativeEvent for the native)
Prevent defaultreturn false worksMust call e.preventDefault()
Where the listener livesOn the elementOn the root container (delegation)
SecurityString is evaluated: XSS risk, blocked by strict CSPNo string evaluation; React refuses string handlers
CleanupManual if set via JSAutomatic on unmount
Propagation pathDOM treeReact tree (portals bubble to React parent)

Calling vs passing

The most common bug in interviews: onClick={handleClick()} calls the function during render and passes its return value. Pass a reference onClick={handleClick}, or wrap it when you need arguments: onClick={() => handleClick(id)}.

this in classes

In class components the handler loses this unless you bind it in the constructor or use a class-field arrow function. Function components do not have this problem.

Example

<!-- Plain HTML -->
<a href="/home" onclick="track('home'); return false;">Home</a>
<script>
  function track(page) { console.log("track", page); }
</script>
// React
function Nav({ onNavigate }) {
  function handleClick(e) {
    e.preventDefault(); // `return false` would do nothing here
    onNavigate("/home");
  }

  return (
    <>
      <a href="/home" onClick={handleClick}>Home</a>
      {/* Wrong: runs on every render, not on click */}
      {/* <button onClick={onNavigate("/about")}>About</button> */}
      <button onClick={() => onNavigate("/about")}>About</button>
    </>
  );
}

Common mistakes

  • onClick={doSomething()} instead of onClick={doSomething}.
  • Writing onclick (lowercase) in JSX; React warns and ignores it.
  • Passing a string: onClick="save()" throws a warning and does nothing.
  • Relying on return false to stop navigation or form submission.

Follow-up questions

  • Does React add an onclick attribute to the DOM? No. Inspect the element: there is no listener on it; React handles it at the root.
  • How do you pass arguments to a handler? Use an arrow function () => remove(id) or read data from e.currentTarget.dataset.
  • Is an inline arrow a performance problem? Usually not; only when passed to memoized children.

Quick recap

  • HTML: lowercase, string code, global scope, return false works.
  • React: camelCase, function reference, closure scope, call preventDefault().
  • React handlers are delegated and auto-cleaned.
  • Pass the function, do not call it.