Handling Events Flashcards

1
Q

What are some syntax differences in react for event handlers?

A

React events are named using camelCase, rather than lowercase.
With JSX you pass a function as the event handler, rather than a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the SyntheticEvent in react?

A

the SyntheticEvent wrapper that forms part of React’s Event System.
Events are synthetic events. React defines these synthetic events according to the W3C spec, so you don’t need to worry about cross-browser compatibility.

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you access the underlying browser event?

A

If you find that you need the underlying browser event for some reason, simply use the nativeEvent attribute to get it. The synthetic events are different from, and do not map directly to, the browser’s native events.

For example in onMouseLeave event.nativeEvent will point to a mouseout event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Synthetic object attributes

A

Every SyntheticEvent object has the following attributes:

boolean bubbles
boolean cancelable
DOMEventTarget currentTarget
boolean defaultPrevented
number eventPhase
boolean isTrusted
DOMEvent nativeEvent
void preventDefault()
boolean isDefaultPrevented()
void stopPropagation()
boolean isPropagationStopped()
void persist()
DOMEventTarget target
number timeStamp
string type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

addEventListener in react

A

When using React, you generally don’t need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using ‘this’ in JSX callbacks

A

You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called.

Generally, if you refer to a method without () after it, such as onClick={this.handleClick}, you should bind that method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Other ways to use besides bind

A

If calling bind annoys you, there are two ways you can get around this. If you are using the experimental public class fields syntax, you can use class fields to correctly bind callbacks:

handleClick = () => {
console.log(‘this is:’, this);
}

If you aren’t using class fields syntax, you can use an arrow function in the callback:

   this.handleClick()}>
    Click me

The problem with this syntax is that a different callback is created each time the LoggingButton renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Best way to solve the ‘this’ issue

A

React devs generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Passing arguments to event handlers

A

Inside a loop, it is common to want to pass an extra parameter to an event handler. For example, if id is the row ID, either of the following would work:

this.deleteRow(id, e)}>Delete Row
Delete Row

The above two lines are equivalent, and use arrow functions and Function.prototype.bind respectively.

In both cases, the e argument representing the React event will be passed as a second argument after the ID. With an arrow function, we have to pass it explicitly, but with bind any further arguments are automatically forwarded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly