JSX (JavaScript XML) and Component Basics in React.js
JSX is a syntax extension for JavaScript that allows you to write HTML-like code within your JavaScript files. In React.js, JSX is used to define the structure and appearance of user interfaces. Components are the building blocks of React applications. Here's an overview of JSX and React components:
JSX (JavaScript XML):
1. JSX Elements:
JSX allows you to define elements that look similar to HTML elements but are actually JavaScript objects representing the DOM elements.
Example of a JSX element:
const element = <h1>Hello, JSX!</h1>;
2. Embedding Expressions:
You can embed JavaScript expressions inside JSX using curly braces {}.
Example:
const name = 'John';
const greeting =
Hello, {name}!
;
3.Attributes in JSX:
JSX attributes use camelCase naming, like className instead of class and onClick instead of onclick.
Example:
const element = <button className="btn" onClick={handleClick}>Click me</button>;
4. Self-Closing Tags:
For elements without children, self-closing tags are used just like in HTML.