DEV Community

MZ
MZ

Posted on

How to pass a variable into your onClick function

Hello everyone, it's been a really long while. Happy new year, firstly!

I've decided to restart doing some daily code challenges again just to refresh my memory and what better way to go about it then to do some Todo project. A simple project that can add, remove and move todo items from a list.

I'll just start off by saying this is completely from an observatory standpoint. It's an interesting one. I got stuck on it for a few minutes, but it's interesting nonetheless.

So here's the frontend code.

<p>Todos</p>
   <ul>
          {
            todosArr.map((todo, id) => {

              return(
                  <div key={id}>
                     <li key={id}>{todo}</li>
                     <button onClick={(id) => onRemove(id)}>Remove</button>
                  </div>
              )
            })
          }
  </ul>
Enter fullscreen mode Exit fullscreen mode

The above code renders the Todos section like this.

Image description

So, from here, I would like to implement the "Remove" button. The way I'm going to go about doing it, is to get the id of each row or text, pass it to a removeItem function and remove it from there.

So, you can roughly see what I'm trying to do here. Get the id and pass it to a function.

todosArr.map((todo, id) => {
   return(
     <div key={id}>
        <li key={id}>{todo}</li>
        <button onClick={(id) => onRemove(id)}>Remove</button>
     </div>
   )
})
Enter fullscreen mode Exit fullscreen mode

This is my onRemove function.

const onRemove = (id) => {
    console.log(`id: ${id}`)
    console.log(id)
    let tmpTodos = [...todosArr]

    tmpTodos.splice(id, 1);

    setTodosArr(tmpTodos)
}
Enter fullscreen mode Exit fullscreen mode

When I click on the "Remove" button, this is what it outputs.

Image description

So, as you can see, it does not output the id, but instead outputs the SyntheticBaseEvent.

Let's say, I were to edit the code as such. I remove the id from the onRemove callback.

So now, instead of

<button onClick={(id) => onRemove(id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

it becomes,

<button onClick={() => onRemove(id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

When I click on the "Remove" button, the function is now correct.

Image description

So, as it turns out, doing it like this,

<button onClick={(id) => onRemove(id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

means you're passing the Event object to the onRemove function.

Ok, cool.

How about we go about it a different way? Let's make it clearer. How about this code?

<button onClick={(event) => onRemove(event, id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

We will explicitly define it as such, which means we would have to modify our onRemove code as well.

Image description

It works.

Image description

Let's change our code again to this.

<button onClick={onRemove}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

Our onRemove function will also change to this.

Image description

So, what does it output?

Interesting. It outputs the event object.

Image description

Alright, so as a recap, all these work.

<button onClick={() => onRemove(id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode
<button onClick={(event) => onRemove(event, id)}>Remove</button>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)