0

I pass the key of my Hook that I map, like this :

Object.entries(userWalletIncomes).map(([key, value]) => {
                        return (
                            <div id="incomeTable" className="text-left mx-auto flex" key={key}>
                                <p className="w-32 border p-1 capitalize">{key} :</p>
                                <p className="w-32 border p-1 text-green-500">{value}€</p>
                                <button onClick={(key) => handleDeleteIncome(key)}><i className="fas fa-times text-red-400 border p-1"></i></button>
                            </div>
                        )
                    })

And on click on the button, I give the key in the call of the function.

When I log it, instead of giving the simple string, it gives me this :

SyntheticBaseEvent {_reactName: "onClick", _targetInst: null, type: "click", nativeEvent: MouseEvent, target: i.fas.fa-times.text-red-400.border.p-1, …}

Inside of this object, there isn't my key. I don't understand why. Where is my mistake ? 🤨

Thanks for helping me understand

2
  • 1
    Just remove (key) from the parameters, then you are good to go ;) I.e leave it like onClick={()=>handleDeleteIncome(key)). What you are doing is that you are passing the event of your click (naming it as key) instead of actually passing the variable you want. Commented Jun 29, 2021 at 14:31
  • @ViktorSandberg Ok I understand better my mistake. Thank you ! Commented Jun 29, 2021 at 14:33

1 Answer 1

1
<button onClick={(event) => handleDeleteIncome(key)}>
  <i className="fas fa-times text-red-400 border p-1"></i>
</button>

You are simply redeclaring the "key" variable in the onClick event.

Sign up to request clarification or add additional context in comments.

1 Comment

Omg... Sometime the answer is so logical we don't even see it. Thank you very much. 🙂

Your Answer

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