Alex Chandler
Alex Chandler

Reputation: 41

Strange second argument passed to event handler (always undefined)

I've attached event listener with onClick property like this:

<a href="#" onClick={function (event) {
    console.log(arguments.length);   // prints 2
    // arguments[0] = SyntheticEvent - it's ok
    // arguments[1] = undefined - what is this???
}}>Click me</a>

The event listener called with 2 arguments: synthetic event and strange undefined value.

I think, normal behavior is when handler called with a single param - event object.

Windows 10. Chrome 53. React version - 15.2.0.

Upvotes: 1

Views: 207

Answers (1)

Boky
Boky

Reputation: 12074

The second argument is the key which react gives to elements when you didn't provided your key.

React will automatically use an increasing integer number for the key.

React will determine whether it is the same component or not based on that key.

Here is more info.

In this jsfiddle you can see if you click on first link that key is 0.0 and if you click on the second one that key is 0.2 because <br/> has a key of 0.1.

Upvotes: 1

Related Questions