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

Why React uses className over class attribute?

Câu trả lời

React uses the className attribute over the class attribute primarily because class is a reserved keyword in JavaScript. Since React leverages JSX, which is an extension of JavaScript, using class directly within JSX would conflict with the JavaScript syntax for defining classes.

JSX is designed to resemble HTML, but it is ultimately transpiled into JavaScript code. When JSX code is processed by Babel or another transpiler, HTML-like syntax, including attributes, is converted into JavaScript objects and function calls. If class were used, it would pose issues during this translation because class has a specific meaning in JavaScript—defining classes since ES2015.

Moreover, the use of className in JSX corresponds to the className property on DOM elements in the underlying JavaScript DOM API. When JSX is transpiled, attributes like className are converted to their respective DOM properties, ensuring that the resulting JavaScript is cle...

middle

middle

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

senior

Does React re-render all components and sub components every time setState
is called?

junior

What is state in React?

middle

Name the different lifecycle methods for a class components

Bình luận

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

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