React Native JSX Basics
JSX is the writing style you use to describe a screen. It looks like HTML tags placed inside JavaScript code. A phone never reads JSX directly. A build tool converts every tag into plain JavaScript calls before the app runs. You write the friendly version, and the tool handles the translation.
What JSX Looks Like
const Welcome = () => {
return (
<View>
<Text>Hello, learner</Text>
</View>
);
};
Each tag stands for a component. View makes a box and Text shows words. The nesting of tags mirrors the nesting on screen.
JSX you write What the phone draws ────────────── ──────────────────── <View> ┌─────────────────┐ <Text>Hello, learner</Text> │ Hello, learner │ </View> └─────────────────┘
Rule 1: Return One Parent
A component must return a single outer element. Two sibling tags at the top level cause an error. Wrap them in a View, or use a Fragment, an empty wrapper that adds nothing to the screen.
return (
<>
<Text>Title</Text>
<Text>Subtitle</Text>
</>
);
Rule 2: Close Every Tag
Every tag needs a closing partner. A tag with no children closes itself with a slash.
<Image source={logo} style={{ width: 50, height: 50 }} />
<TextInput placeholder="Email" />
Rule 3: Use Curly Braces for JavaScript
Curly braces open a window into JavaScript. Place any variable, calculation, or function call inside them.
const name = 'Ravi';
const items = 3;
<Text>Welcome, {name}</Text>
<Text>Total: {items * 100} rupees</Text>
Text with braces: Welcome, {name}
│
JavaScript fills it in
▼
On screen: Welcome, Ravi
Rule 4: Use camelCase Names
JSX attributes follow camelCase spelling. Web developers write background-color, and React Native uses backgroundColor. Event handlers follow the same pattern: onPress, onChangeText, onLongPress.
Passing Props
Attributes on a component act as inputs called props. A string goes in quotes. Every other value goes in curly braces.
<Greeting name="Mina" age={22} isMember={true} />
Showing Content Conditionally
Screens often change based on data. JSX supports two compact patterns for this.
The AND Pattern
Show an element only when a condition is true.
{isLoggedIn && <Text>Welcome back</Text>}
The Ternary Pattern
Pick one of two elements.
{isLoggedIn ? <Text>Logout</Text> : <Text>Login</Text>}
isLoggedIn = true → Logout isLoggedIn = false → Login
Rendering Lists
Use map to turn an array into a group of components. Give each item a unique key so React can track it when the list changes.
const fruits = ['Apple', 'Mango', 'Banana'];
{fruits.map((fruit) => (
<Text key={fruit}>{fruit}</Text>
))}
['Apple', 'Mango', 'Banana']
│
map()
▼
<Text>Apple</Text>
<Text>Mango</Text>
<Text>Banana</Text>
Comments Inside JSX
A normal JavaScript comment breaks inside a tag tree. Wrap the comment in curly braces.
<View>
{/* This note stays hidden from users */}
<Text>Visible text</Text>
</View>
Common Mistakes
- Writing raw words outside a
Textcomponent - Forgetting the
keyprop in a list - Returning two sibling elements without a wrapper
- Using
classorstyle="..."strings from web habits
Summary
JSX lets you describe a screen with tags inside JavaScript. Return one parent, close every tag, and use curly braces for dynamic values. Combine the AND pattern, the ternary pattern, and map to build screens that react to data.
