How can you stop an event from propagating to parent elements in React?
- By calling e.preventDefault() within the event handler.
- By using the stopPropagation() method on the event object: e.stopPropagation();
- By setting e.cancelBubble = true; in the event handler.
- It's not possible to stop event propagation in React.
To stop an event from propagating to parent elements in React, you should use the stopPropagation() method on the event object, as described in option 2. This method prevents the event from continuing to bubble up the component tree. Option 1 is incorrect, as e.preventDefault() is used to prevent the default behavior of an event, not its propagation. Option 3 is not the recommended way in React, and option 4 is not accurate, as event propagation can indeed be stopped in React.
Loading...
Related Quiz
- In a complex React application with various interconnected components, you want to avoid unnecessary re-renders while sharing state and logic. Which React pattern would be most suitable to achieve this objective?
- React Portals provide a way to render children into a DOM node that exists ________ the DOM hierarchy of the parent component.
- What is the purpose of using the useQuery hook provided by Apollo Client in a React application?
- In React Router, which component is primarily used to define a specific path and its corresponding component?
- Why we need to pass a function to setState()?