JSX (JavaScript XML) is a syntax extension used in React Native that allows you to write UI code that looks similar to HTML inside JavaScript. It makes component structure easier to read and understand while still using the power of JavaScript.
React.createElement(){}In React Native, JSX is used to describe UI using components like View, Text, and Image. Unlike web React, React Native does not use HTML tags such as div or span.
// Basic JSX structure in a React Native component
import React from 'react';
import { View, Text } from 'react-native';
const App = () => {
return (
<View>
<Text>Hello JSX</Text>
</View>
);
};
export default App;
The above JSX renders a simple screen with the text Hello JSX. JSX tags are converted into native UI components by React Native at runtime.
Concept: Conditional Rendering. Click the button below to see how JSX dynamically switches text using a ternary operator ? :.
React Native Developer
// 1. State Variable
const [isFollowing, setFollowing] = useState(false);
// 2. JSX Return
<Button
title={ isFollowing ? "Unfollow" : "Follow" }
onPress={() => setFollowing(!isFollowing)}
/>
{} to insert JavaScript expressionsView