What is JSX?
JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows you to write HTML-like code within your JavaScript files. JSX makes it easier to write and add HTML in React and React Native applications.
Key Concepts of JSX
- Embedding Expressions: You can embed any JavaScript expression within JSX 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.
- Specifying Attributes with JSX: You can use quotes to specify string literals as attributes and curly braces to embed JavaScript expressions.
- JSX Prevents Injection Attacks: React DOM escapes any values embedded in JSX before rendering them. This ensures that you can never inject anything that’s not explicitly written in your application.
- JSX Represents Objects: Babel compiles JSX down to
React.createElement()
calls.
Basic Example
Here is a simple example of JSX:
This JSX code is transformed into the following JavaScript code:
Embedding Expressions in JSX
You can embed any JavaScript expression in JSX by wrapping it in curly braces {}
. For example:
This will render as:
JSX is an Expression Too
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects. This means you can use JSX inside if statements and for loops, assign it to variables, accept it as arguments, and return it from functions.
Example
function getGreeting(user) { if (user) { return <h1>Hello, {user.name}!</h1>; } return <h1>Hello, Stranger.</h1>; }
Specifying Attributes with JSX
You can use quotes to specify string literals as attributes:
You can also use curly braces to embed a JavaScript expression in an attribute:
JSX Prevents Injection Attacks
By default, React DOM escapes any values embedded in JSX before rendering them. This ensures that you can never inject anything that’s not explicitly written in your application.
Example
const title = response.potentiallyMaliciousInput; // This is safe: const element = <h1>{title}</h1>;
JSX Represents Objects
Babel compiles JSX down to React.createElement()
calls. These two examples are identical:
JSX
JavaScript
Practical Exercise
Task
Create a simple React Native component that uses JSX to display a greeting message. The message should include a name passed as a prop.
Solution
import React from 'react'; import { Text, View } from 'react-native'; const Greeting = (props) => { return ( <View> <Text>Hello, {props.name}!</Text> </View> ); }; export default function App() { return ( <View style={{padding: 50}}> <Greeting name="John" /> <Greeting name="Jane" /> </View> ); }
Explanation
- Greeting Component: This functional component takes
props
as an argument and returns aView
containing aText
element. TheText
element displays a greeting message that includes thename
prop. - App Component: This is the main component of the application. It renders two
Greeting
components with different names.
Common Mistakes
- Forgetting Curly Braces: When embedding JavaScript expressions in JSX, always remember to use curly braces
{}
. - Invalid HTML Attributes: Ensure that you use valid HTML attributes. For example, use
className
instead ofclass
. - Unclosed Tags: Always close your tags. For example,
<img />
instead of<img>
.
Conclusion
In this section, you learned about JSX, a syntax extension for JavaScript that allows you to write HTML-like code within your JavaScript files. You explored how to embed expressions, specify attributes, and prevent injection attacks using JSX. You also created a simple React Native component to practice using JSX. Understanding JSX is crucial for building React Native applications, as it forms the foundation for writing components and rendering UI elements.
React Native Course
Module 1: Introduction to React Native
- What is React Native?
- Setting Up the Development Environment
- Hello World App
- Understanding JSX
- Components and Props
Module 2: Core Components and Styling
- Core Components Overview
- Text, View, and Image
- Styling with Flexbox
- Handling User Input
- ScrollView and ListView
Module 3: State and Lifecycle
- State and Lifecycle Methods
- Handling Events
- Conditional Rendering
- Lists and Keys
- Forms and Controlled Components
Module 4: Navigation
- Introduction to React Navigation
- Stack Navigator
- Tab Navigator
- Drawer Navigator
- Passing Parameters to Routes
Module 5: Networking and Data
- Fetching Data with Fetch API
- Using Axios for HTTP Requests
- Handling Network Errors
- AsyncStorage for Local Data
- Integrating with REST APIs
Module 6: Advanced Concepts
Module 7: Deployment and Publishing
- Building for iOS
- Building for Android
- Publishing to App Store
- Publishing to Google Play
- Continuous Integration and Delivery