01 December 2024
Feature | JSX/TSX | HTML |
---|---|---|
Syntax | Combines JavaScript/TypeScript with XML-like tags. | Pure markup language. |
Attributes | Uses camelCase (e.g., className , onClick ). | Uses standard HTML attributes (e.g., class , onclick ). |
Dynamic Content | Allows embedding of JavaScript expressions using {} . | Requires separate scripting with <script> tags. |
Rendering | Transpiles to React.createElement() calls to render the DOM. | Directly rendered by the browser. |
Custom Components | Supports reusable React components as tags. | Does not support custom tags unless extended by frameworks. |
Type Safety | TSX supports type-checking for props and components. | HTML has no type-checking capabilities. |
Commonly Used Attributes
class
with className
to define CSS classes.<div className="container">Hello, World!</div>
for
with htmlFor
in <label>
tags.<label htmlFor="name">Name:</label>
<input id="name" type="text" />
<div style={{ color: 'blue', fontSize: '18px' }}>Styled Text</div>
Dynamic Content with {}
const name = "John";
<h1>Hello, {name}!</h1> // Output: Hello, John!
Event Handlers
<button onClick={() => alert('Button clicked!')}>Click Me</button>
Conditional Rendering
&&
inside JSXconst isLoggedIn = true;
<p>{isLoggedIn ? "Welcome back!" : "Please log in."}</p>
Spreading Props
...
operatorconst props = { id: "name", placeholder: "Enter your name" };
<input {...props} />;
TSX-Specific Features
type ButtonProps = { label: string; onClick: () => void };
const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
<button onClick={onClick}>{label}</button>
);
camelCase
attributes, dynamic expressions, and type safety make it much more flexible and suited for modern application development.