Câu hỏi phỏng vấn Reactjs
Câu hỏi

What is the difference between HTML and React event handling?

Câu trả lời

The differences between event handling in HTML and React primarily revolve around syntax, the underlying principles of how events are managed, and the integration with the DOM.

Syntax Differences

  1. Event Naming: In HTML, event names are written in lowercase, such as onclick or onsubmit. In contrast, React uses camelCase for event names, such as onClick or onSubmit[1][2][3].
  2. Event Handler Assignment: In HTML, event handlers are typically assigned as strings directly within the HTML tags, e.g., <button onclick="activateLasers()">. However, in React, event handlers are assigned using curly braces and involve passing a function reference, e.g., <button onClick={activateLasers}>[2][3][8].
  3. Default Behavior: To prevent default behavior in HTML, you can return false from the handler. In React, you must explicitly call the preventDefault method on the event object passed to the handler function[2][3][8].

Integration with the DOM

  • Direct DOM Manipulation: HTML allows direct manipulation of the DOM. Event handlers can directly alter DOM elements and their attributes. React, however, does not encourage direct DOM manipulations as it uses a virtual DOM to optimize rendering. Changes in React are made to the state or props of components, which React then reflects in the DOM efficiently[1][3][8].
  • Adding Event Listeners: In traditional HTML and JavaScript, event listeners can be added using addEventListener and removed with removeEventListener. React abstracts this process; you provide event handlers when the component renders, and React handles the creation and cleanup of these listeners behind the scenes[3][8].

Performance and Optimization

  • Virtual DOM: React's ...
middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What are refs used for in React?

senior

Why would you need to bind event handlers to this ?

middle

How to bind methods or event handlers in JSX callbacks?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào