Introduction
JSX (JavaScript Syntax Extension) is a syntax extension for JavaScript that looks similar to XML or HTML. It is used with React to describe what the UI should look like. JSX makes it easier to write and add HTML in React.
Key Concepts
- Embedding Expressions in JSX
JSX allows you to embed any JavaScript expression by wrapping it in curly braces {}
.
- JSX is an Expression Too
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
const element = <h1>Hello, world!</h1>; // Compiles to: const element = React.createElement('h1', null, 'Hello, world!');
- Specifying Attributes with JSX
You can use quotes to specify string literals as attributes and curly braces to embed a JavaScript expression.
const element = <img src="logo.png" alt="Logo" />; const elementWithExpression = <img src={user.avatarUrl} alt={user.name} />;
- JSX Represents Objects
Babel compiles JSX down to React.createElement()
calls.
const element = ( <h1 className="greeting"> Hello, world! </h1> ); // Compiles to: const element = React.createElement( 'h1', { className: 'greeting' }, 'Hello, world!' );
Practical Examples
Example 1: Embedding Expressions
import React from 'react'; import ReactDOM from 'react-dom'; const user = { firstName: 'John', lastName: 'Doe' }; function formatName(user) { return user.firstName + ' ' + user.lastName; } const element = ( <h1> Hello, {formatName(user)}! </h1> ); ReactDOM.render( element, document.getElementById('root') );
Example 2: Specifying Attributes
import React from 'react'; import ReactDOM from 'react-dom'; const element = <img src="https://via.placeholder.com/150" alt="Placeholder Image" />; ReactDOM.render( element, document.getElementById('root') );
Example 3: JSX Represents Objects
import React from 'react'; import ReactDOM from 'react-dom'; const element = ( <div> <h1>Hello, world!</h1> <h2>It is {new Date().toLocaleTimeString()}.</h2> </div> ); ReactDOM.render( element, document.getElementById('root') );
Exercises
Exercise 1: Embedding Expressions
Create a React component that displays a greeting message using a user's first and last name.
Solution:
import React from 'react'; import ReactDOM from 'react-dom'; const user = { firstName: 'Jane', lastName: 'Doe' }; function formatName(user) { return user.firstName + ' ' + user.lastName; } const element = ( <h1> Hello, {formatName(user)}! </h1> ); ReactDOM.render( element, document.getElementById('root') );
Exercise 2: Specifying Attributes
Create a React component that displays an image with a specified src
and alt
attribute.
Solution:
import React from 'react'; import ReactDOM from 'react-dom'; const element = <img src="https://via.placeholder.com/150" alt="Placeholder Image" />; ReactDOM.render( element, document.getElementById('root') );
Common Mistakes and Tips
-
Mistake: Forgetting to close JSX tags.
- Tip: Always close JSX tags, even self-closing ones like
<img />
.
- Tip: Always close JSX tags, even self-closing ones like
-
Mistake: Using
class
instead ofclassName
.- Tip: Use
className
for specifying CSS classes in JSX.
- Tip: Use
-
Mistake: Embedding expressions without curly braces.
- Tip: Always wrap JavaScript expressions in curly braces
{}
.
- Tip: Always wrap JavaScript expressions in curly braces
Conclusion
JSX is a powerful syntax extension that allows you to write HTML-like code within JavaScript. It simplifies the process of creating React elements and makes your code more readable. Understanding JSX is fundamental to working with React, as it is the primary way to define the structure of your components.
React Course
Module 1: Introduction to React
- What is React?
- Setting Up the Development Environment
- Hello World in React
- JSX: JavaScript Syntax Extension
Module 2: React Components
- Understanding Components
- Functional vs Class Components
- Props: Passing Data to Components
- State: Managing Component State
Module 3: Working with Events
Module 4: Advanced Component Concepts
- Lifting State Up
- Composition vs Inheritance
- React Lifecycle Methods
- Hooks: Introduction and Basic Usage
Module 5: React Hooks
Module 6: Routing in React
Module 7: State Management
- Introduction to State Management
- Context API
- Redux: Introduction and Setup
- Redux: Actions and Reducers
- Redux: Connecting to React
Module 8: Performance Optimization
- React Performance Optimization Techniques
- Memoization with React.memo
- useMemo and useCallback Hooks
- Code Splitting and Lazy Loading
Module 9: Testing in React
- Introduction to Testing
- Unit Testing with Jest
- Testing Components with React Testing Library
- End-to-End Testing with Cypress
Module 10: Advanced Topics
- Server-Side Rendering (SSR) with Next.js
- Static Site Generation (SSG) with Next.js
- TypeScript with React
- React Native: Building Mobile Apps