What Is JSX, TSX and How It Differs From HTML
- JSX (JavaScript XML): A syntax extension for JavaScript used in React to describe the UI structure.
- TSX (TypeScript XML): Similar to JSX but used in React projects with TypeScript for type safety.
- Both allow developers to write code that looks like HTML, but they are not HTML—they are syntactic sugar that gets transpiled into JavaScript.
Key Differences Between JSX/TSX and HTML
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. |
JSX/TSX Attributes and How to Use Them
-
Commonly Used Attributes
- className: Replace
class
withclassName
to define CSS classes.
<div className="container">Hello, World!</div>
- htmlFor: Replace
for
withhtmlFor
in<label>
tags.
<label htmlFor="name">Name:</label> <input id="name" type="text" />
- style: Use a JavaScript object for inline styles.
<div style={{ color: 'blue', fontSize: '18px' }}>Styled Text</div>
- className: Replace
-
Dynamic Content with
{}
- Embed JavaScript expressions inside curly braces:
const name = "John"; <h1>Hello, {name}!</h1> // Output: Hello, John!
-
Event Handlers
- Pass JavaScript functions to event attributes
<button onClick={() => alert('Button clicked!')}>Click Me</button>
-
Conditional Rendering
- Use ternary operators or logical
&&
inside JSX
const isLoggedIn = true; <p>{isLoggedIn ? "Welcome back!" : "Please log in."}</p>
- Use ternary operators or logical
-
Spreading Props
- Spread attributes dynamically using the
...
operator
const props = { id: "name", placeholder: "Enter your name" }; <input {...props} />;
- Spread attributes dynamically using the
-
TSX-Specific Features
- Type-check props
type ButtonProps = { label: string; onClick: () => void }; const Button: React.FC<ButtonProps> = ({ label, onClick }) => ( <button onClick={onClick}>{label}</button> );
Summary
- JSX/TSX provides a declarative, powerful way to describe UI with JavaScript/TypeScript.
- While it resembles HTML, key differences like
camelCase
attributes, dynamic expressions, and type safety make it much more flexible and suited for modern application development.