日本語

How to Fix “Expected onClick listener to be a function” in React

​​React​Troubleshooting,TypeScript,Vite

When I wrote a click handler (an event handler) for a button in React, the browser console showed Expected `onClick` listener to be a function, instead got a value of `string` type. This happens when you pass a string to onClick instead of a function. It occurs if you write onClick="handleClick()", the same way you would with the HTML onclick attribute. Passing the function itself inside curly braces, as in onClick={handleClick}, removes the error and makes the button work.

In my test environment, I caused this error on purpose with a component where pressing a button increases a counter. I confirmed the message that appeared in the browser console, and that it disappeared after I fixed the code.

Note

The test environment was Ubuntu 24.04 with Node.js 22.23.4. I used a project created with the react-ts template from npm create vite@latest (react and react-dom 19.3.1, Vite 8.3.8, TypeScript 7.0.2, oxlint 1.93.0), and wrapped the component in StrictMode. I opened the page with Chromium 153 operated through Playwright 1.63.0. The DevTools screenshots were taken by running Chromium with a display under a virtual display (Xvfb). I did not test frameworks such as Next.js, or browsers other than Chromium.

The message shown when a string is passed to onClick

I opened the following component with the development server (vite, started by npm run dev). Following the HTML onclick attribute, I wrote the string "handleClick()" for onClick.

tsx
// Mistake: passing a string to onClick as if it were the HTML onclick attribute
import { useState } from 'react';

export default function BadString() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <>
      <button onClick="handleClick()">追加</button>
      <p>押した回数: {count}</p>
    </>
  );
}

Just opening the page showed one error in the DevTools console:

text
Expected `onClick` listener to be a function, instead got a value of `string` type.

This means that the onClick listener (the function that receives the event) should be a function, but a value of type string was passed. The screen was not blank: the button and the text "Clicks: 0" were displayed.

When I then pressed the button, the following error appeared each time. This one starts with Uncaught Error:.

text
Uncaught Error: Expected `onClick` listener to be a function, instead got a value of `string` type.

Even after I pressed the button three times, the display still showed "Clicks: 0". handleClick was never called.

The DevTools console shows two red lines. The first line reads "Expected `onClick` listener to be a function, instead got a value of `string` type." The second reads "Uncaught Error: Expected `onClick` listener to be a function, instead got a value of `string` type." Above the console, the page shows an "Add" button and the text "Clicks: 0". The labels in the image are in Japanese.
The page with the string passed, after pressing the button once. One error appeared when the page opened and one when the button was pressed.

The error at page load and at click come from different places

The same message appears twice because react-dom checks at two different points. In the development build of react-dom 19.3.1, the code works as follows:

  • During rendering: if a value that is not a function is set for onClick, it outputs the message above with console.error. Processing does not stop, so the page is still displayed.
  • When the button is pressed: inside getListener, which takes the listener (the function that receives the event) from the props, it throws an Error if the value is not a function. This is the Uncaught Error: line.

Also, the rendered button HTML was <button>追加</button>, with no onclick attribute. In React, a string written in onClick does not become the HTML onclick attribute, and the code inside the string does not run.

Passing the function itself removes the error

As the official documentation on event handlers explains, you define a function and pass it to onClick inside curly braces. I changed "handleClick()" to {handleClick}.

diff
-      <button onClick="handleClick()">追加</button>
+      <button onClick={handleClick}>追加</button>

Do not add () after handleClick. If you do, the function is called on every render instead of when the button is pressed. (The official documentation also says that functions should be passed, not called.)

I opened the fixed component in the same development server and pressed the button three times. No error appeared in the console, and the display changed to "Clicks: 3".

On the fixed page, "Clicks: 3" appears below the "Add" button. The DevTools console below shows only one line, a message from React DevTools. The labels in the image are in Japanese.
The page with the function passed, after pressing the button three times. No error appeared.

The one line left in the console is a React DevTools notice. It is not related to the error, and it also appeared on the page before the fix.

Using && to remove the handler gives a different message

If you want to add or remove a click handler depending on a condition, writing {enabled && handleClick} passes false to onClick when enabled is false. When I set enabled to false in the test environment and opened the page, a different message appeared from the string case.

tsx
const enabled = false;
// …
<button onClick={enabled && handleClick}>追加</button>
text
Expected `onClick` listener to be a function, instead got `false`.

If you used to conditionally omit it with onClick={condition && value}, pass onClick={condition ? value : undefined} instead.

The second line says: "If you used to conditionally omit it with onClick={condition && value}, pass onClick={condition ? value : undefined} instead." When I pressed the button, the next error appeared:

text
Uncaught TypeError: listener is not a function

Unlike the string case, getListener returns false as it is. React then tries to call it as a function, which causes a JavaScript TypeError. This message does not contain the word onClick, so I used the message shown when the page opened to find which event prop was the cause.

As the message suggests, I changed the code to pass undefined with a ternary operator.

diff
-      <button onClick={enabled && handleClick}>追加</button>
+      <button onClick={enabled ? handleClick : undefined}>追加</button>

On the fixed page, no error appeared in the console when the page opened or when I pressed the button three times. Because enabled is false, the display still showed "Clicks: 0".

TypeScript catches both cases before you open the browser

If you write this in TypeScript, both forms are type-check errors. When I ran npx tsc -p tsconfig.app.json --noEmit in the test project, it output the following:

text
src/BadFalse.tsx(14,15): error TS2322: Type 'boolean' is not assignable to type 'MouseEventHandler<HTMLButtonElement>'.
src/BadString.tsx(7,12): error TS6133: 'handleClick' is declared but its value is never read.
src/BadString.tsx(13,15): error TS2322: Type 'string' is not assignable to type 'MouseEventHandler<HTMLButtonElement>'.

The TS2322 errors mean that the type accepted by onClick is MouseEventHandler<HTMLButtonElement> (a function that receives a mouse event), so string and boolean cannot be passed. The string version also produced error TS6133, which says handleClick is never used. Since "handleClick()" is a string, it does not use the function.

oxlint, which the template includes, also gave a warning (no-unused-vars) that handleClick is unused when I ran npx oxlint src.

However, as the Vite documentation explains, the development server does not perform type checking. In this case, I could open the page on the development server even though the files had type errors. The template's npm run build runs tsc -b first. When I ran npm run build in the test environment, it exited with code 1 and the same three errors, and no production files (dist) were created.

The production build shows only an error number

I skipped the type check and built for production with npx vite build alone, then opened the result with npx vite preview. The error at page load no longer appeared, but pressing the button produced this error:

text
Uncaught Error: Minified React error #231; visit https://react.dev/errors/231?args[]=onClick&args[]=string for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

In the table of error numbers and messages in the React repository, number 231 is Expected `%s` listener to be a function, instead got a value of `%s` type. The #231 error occurs when a non-function value, such as a string or an object, is passed as an event prop. The args[]= parameters in the URL contain the prop name (onClick) and the type of the value passed (string).

On the page where I passed false with &&, the error on click was Uncaught TypeError: a is not a function. The variable name listener was shortened by the production build. Since the message has neither a number nor the word onClick, the message alone does not show which prop caused the error.

​Posted by 86