Events in React Flashcards

1
Q

What is event propagation, and how does it work in the DOM?

A

Event propagation refers to the process by which events travel through the DOM tree. In the DOM, events go through two phases: the capturing phase, where the event travels from the root element to the target element, and the bubbling phase, where it travels back up from the target to the root.

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

How does event delegation work in the context of handling events in the DOM?

A

Event delegation is a technique where a single event handler is placed on a parent element to handle events for multiple child elements. When an event occurs, it bubbles up to the parent element, where you can determine which child element triggered the event using the event’s target property.

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

Why is event delegation important in web development?

A

Event delegation helps improve performance and memory usage in situations where you need to handle events for a large number of elements. Instead of attaching individual event handlers to each element, you can delegate the event handling to a common parent element.

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

How does React handle events differently from native JavaScript?

A

React uses synthetic events, which are wrappers around native DOM events, to ensure consistent behaviour across different browsers. React registers event handlers at the root DOM container and delegates events to the appropriate components based on the target element.

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

What is a synthetic event in React?

A

A synthetic event in React is a wrapper around a native DOM event object. It provides a consistent interface for handling events across different browsers and allows React to manage event delegation efficiently.

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

What are some key differences between event handling in React and vanilla JavaScript?

A

In React, event handler prop names are in camelCase (e.g., ‘onClick’), preventing the browser’s default behaviour is done using ‘preventDefault()’ on the synthetic event object, and capturing phase handling can be specified using the ‘Capture’ suffix (e.g., ‘onClickCapture’). Additionally, returning ‘false’ from a React event handler does not prevent the browser’s default behaviour.

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

How does React ensure consistent event behaviour across different browsers?

A

React uses synthetic events to abstract away browser inconsistencies, ensuring that events behave consistently in all supported browsers. Synthetic events make it easier for developers to work with events across different platforms.

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

What is the purpose of event bubbling in React, and why is it important?

A

Event bubbling in React ensures that events registered at the root container can efficiently delegate to the appropriate components, improving performance and maintaining a consistent event handling model across the application.

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

When might you need to use the ‘stopPropagation’ method in event handling?

A

You can use the ‘stopPropagation’ method to prevent an event from further propagation (both capturing and bubbling phases). This might be necessary when you want to stop an event from reaching parent or sibling elements, but it should be used sparingly as it can affect the normal flow of events.

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