0

I have a simple button tag on a next.js main page.js file that alerts the window after clicking. The onClick function works on any desktop browser but doesn't work on any mobile browser.

"use client";

export default function Page() {
  return (
    <button onClick={() => window.alert("Hello!")}>
      Hello!
    </button>
  );
}

I tried both onClick and onTouchStart and also tried the button with "cursor: pointer" and nothing worked. This question might be a duplicated but the duplicate ones are unanswered. Any help would be appreciated.

Amir B
  • 28
  • 5
  • In case anyone faced the same issue, It randomly started working the next day and I have no idea why although I investigated many different reasons. All I know is that it was an issue on next js side. – Amir B Jul 27 '23 at 07:05

1 Answers1

0

Have you tried to put both onClick and onTouchStart? In an old project i did that:

  const onClickHandler = () => {
      window.alert("Hello!")
  };

  const onTouchStartHandler = (event) => {
      event.preventDefault();
      onClickHandler();
  };

  return (
      <button onClick={onClickHandler} onTouchStart={onTouchStartHandler}>
          CLICK
      </button>
  );
Talon
  • 351
  • 1
  • 11