2

my function is like this:

async function handleFavorites(event: MouseEvent) {
 event.preventDefault();
}

on my button, I'm using onclick to call this function but it's not working

<button
 type="submit"
 value={favorites}
 onChange={e => setFavorites(e.currentTarget.value)}
 onClick={() => { 
   handleFavorites();
 }}
>

Error: Expected 1 arguments, but got 0. An argument for 'event' was not provided.

does anyone know how to solve? or have any tips?

4
  • 1
    onClick={e => handleFavorites(e)}? Commented Jan 31, 2021 at 15:16
  • already tried like this, there is a typing problem Commented Jan 31, 2021 at 16:02
  • 1
    Then there’s a problem with the function signature. It should probably take a MouseEvent<HTMLButtonElement>. Commented Jan 31, 2021 at 16:54
  • Did you ever find a good solution for this? I have the exact same issue with the typescript event for a button click not having the target.value property Commented Jun 23, 2021 at 23:50

4 Answers 4

4

The problem is that you are not passing the event argument to your handler. This should work:

<button
 type="submit"
 value={favorites}
 onChange={e => setFavorites(e.currentTarget.value)}
 onClick={handleFavorites}
>
Sign up to request clarification or add additional context in comments.

2 Comments

already tried like this, there is a typing problem
In that case, MouseEvent is not the correct type. Usually typescript will tell you what type is expected. You could try: MouseEvent<HTMLButtonElement>, or FormEvent<HTMLFormElement>.
4

React has its own "synthetic events" which wrap the underlying native DOM events. The core of your problem is the confusion between the synthetic event and the native event. Your handleFavorites method is currently expecting a native event, while the button onClick provides a synthetic event. With your current types, you could actually do this onClick={e => handleFavorites(e.nativeEvent)}, though I don't recommend it.

Everyone else is correct that you want to pass your callback as onClick={handleFavorites}. But in order to do that, your handleFavorites needs to accept a synthetic event. React and the DOM both have their own interfaces for a MouseEvent with the same name, so you need to make sure that you are using the right one.

  1. You can change the type to (event: React.MouseEvent) which makes it clear that this is react synthetic event.
  2. You can keep (event: MouseEvent) and import the react version into your file import React, {MouseEvent} from "react";

Note that the native event and the synthetic event both have a preventDefault() method, so switching the type won't necessitate changes in the function.

Comments

2

You don't need to write functions to onclick again. Try it this way.

function handleFavorites(e) {
 e.preventDefault();
}

onClick={handleFavorites}

1 Comment

I need to use FormEvent in the function, the typing problem
1

onClick={(e) => { handleFavorites(e); }}

Or

onClick={handleFavorites}

2 Comments

already tried like this, there is a typing problem
Maybe from event type in the function. Try setting Its type to any instead of MouseEvent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.