-
Notifications
You must be signed in to change notification settings - Fork 1
Chapter04 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Chapter04 #11
Conversation
rlobenwein
commented
Oct 4, 2025
- I've included event handler examples to enhance understanding.
- I've suggested expanding the event propagation explanation and included a code example there. I'll put the text suggestion in a separate comment here.
- Event pooling was removed in React 17. The section from the chapter and the code example should be removed.
|
This is the suggestion for the Event propagation section: Event PropagationIn JavaScript, events usually propagate in two ways:
<div onclick="console.log('Div clicked')">
<button onclick="console.log('Button clicked')">Click me</button>
</div>This happens because the event bubbles up from the button to its parent div. In React, event propagation follows the same model, bubbling and capturing, but they are attached differently: while traditional JavaScript attaches a separate listener directly to every single DOM element (e.g., 100 buttons = 100 listeners), React attaches a single native event listener to the root of the React application (usually the document or the element where
function EventPropagationDemo() {
const handleGrandparent = (e) => {
alert('Grandparent - bubbling phase');
};
const handleParent = (e) => {
alert('Parent - bubbling phase');
};
const handleChild = (e) => {
alert('Child - target');
};
const handleParentCapture = (e) => {
alert('Parent - capture phase');
};
const handleGrandparentCapture = (e) => {
alert('Grandparent - capture phase');
};
return (
<>
<h2>Event Propagation: capturing and bubbling</h2>
<div onClick={handleGrandparent} onClickCapture={handleGrandparentCapture} style={{ padding: "20px", border: "2px solid black" }}>
<p>Grandparent</p>
<div onClick={handleParent} onClickCapture={handleParentCapture} style={{ padding: "20px", border: "1px solid black" }}>
<p>Parent</p>
<button onClick={handleChild}>(Child) Click Me</button>
</div>
</div>
</>
);
}
export default EventPropagationDemo;You can use function EventPropagationDemo() {
const handleGrandparent = (e) => {
alert('Grandparent - bubbling phase');
};
const handleParent = (e) => {
alert('Parent - bubbling phase');
};
const handleChild = (e) => {
alert('Child - target');
e.stopPropagation();
};
const handleParentCapture = (e) => {
alert('Parent - capture phase');
};
const handleGrandparentCapture = (e) => {
alert('Grandparent - capture phase');
};
return (
<>
<h2>Event Propagation: capturing and bubbling</h2>
<div onClick={handleGrandparent} onClickCapture={handleGrandparentCapture} style={{ padding: "20px", border: "2px solid black" }}>
<p>Grandparent</p>
<div onClick={handleParent} onClickCapture={handleParentCapture} style={{ padding: "20px", border: "1px solid black" }}>
<p>Parent</p>
<button onClick={handleChild}>(Child) Click Me</button>
</div>
</div>
</>
);
}
export default EventPropagationDemo;React's event delegation provides several benefits. First, it reduces the number of event listeners attached to individual DOM elements, resulting in improved performance. Second, it allows you to handle events for dynamically created or removed elements without worrying about attaching or detaching event listeners manually. |