Q5 ReactJs
Q5 ReactJs
JSX (JavaScript XML) looks similar to HTML but has some key differences:
JSX requires self-closing tags for elements like <img /> and <br />.
JSX uses camelCase for attributes (e.g., className instead of class, onClick instead of
onclick).
JSX allows embedding JavaScript expressions inside {}.
JSX must be wrapped in a single parent element (or a fragment).
No, browsers cannot understand JSX directly because it's not valid JavaScript. JSX is transpiled
into regular JavaScript using tools like Babel.
For example:
This is why JSX must be processed before browsers can run it.
Fragments allow you to group multiple elements without adding an extra node to the DOM.
Instead of:
The key attribute helps React identify elements efficiently when updating the DOM.
Example usage in lists:
const items = ["Apple", "Banana", "Cherry"]; return ( <ul> {items.map((item, index) =>
( <li key={index}>{item}</li> ))} </ul> );
Without key, React may not update items correctly, leading to performance issues.