Skip to content
Advertisement

Why is this onClick event handler firing twice in my create-react-app

can someone tell me why is this “upvote” onClick handler firing twice? the logs would indicate it’s only running once but the score it controls increases by 2

JavaScript

on the other hand if I rewrite the handler this way, then it fires only once

JavaScript

Advertisement

Answer

My best guess is that in the first case, you are also modifying the state directly, when you do joke.score = joke.score + 1;

Because you are doing this mapping directly on state array variable, and in Javascript, when using array, you are only working with pointer to that array, not creating a copy of that array.

So the mapping function probably takes a shallow copy of the array, and there’s where problem happens.

You can use lodash to create a deep copy of the state array before you going to work with it, which will not cause your problem:

https://codesandbox.io/s/great-babbage-lorlm

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement