Skip to main content
React Native Introduction
CHAPTER 06 Beginner

JSX and Components in React Native

Updated: May 16, 2026
5 min read

# CHAPTER 6

JSX and Components in React Native

1. Introduction

If you open an old Android app written in Java, you will find UI files separated from logic files. In React Native, UI and logic live together in the exact same file. How is it possible to write visual UI tags inside a pure JavaScript file? The answer is JSX. In this chapter, we will master JSX and Components. We will learn the strict rules of JSX, how to build reusable Functional Components, and how to snap them together like LEGO bricks using Component Composition.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain what JSX is and why it exists.
  • Understand the 3 fundamental rules of writing JSX.
  • Create custom Functional Components from scratch.
  • Embed JavaScript variables and logic directly into your UI.
  • Use Component Composition to build complex screens.

3. What is JSX?

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows you to write HTML-like code inside JavaScript files. Under the hood, a compiler called Babel takes your JSX tags and translates them into standard JavaScript functions (like React.createElement()) before the code runs on the phone.

4. The 3 Golden Rules of JSX

Writing JSX looks like HTML, but it is much stricter. If you violate these rules, the Metro Bundler will instantly crash.

Rule 1: Return a Single Parent Element You cannot return two sibling elements (like a Title and a Subtitle) side-by-side. They MUST be wrapped inside a single parent tag.

javascript
12345678910111213
// BAD (Will Crash)
return (
  <Text>Title</Text>
  <Text>Subtitle</Text>
);

// GOOD (Wrapped in a View)
return (
  <View>
    <Text>Title</Text>
    <Text>Subtitle</Text>
  </View>
);

Rule 2: Close All Tags In HTML, you can leave an image tag unclosed: <img src="url">. In JSX, EVERY tag must be explicitly closed, either with a closing tag </View> or a self-closing slash />.

javascript
12345
// BAD
<Image source={require(&#039;./logo.png&#039;)}> 

// GOOD
<Image source={require(&#039;./logo.png&#039;)} />

Rule 3: Use camelCase for Properties HTML uses standard lowercase attributes. JSX requires camelCase. (e.g., onclick becomes onPress, tabindex becomes tabIndex).

5. Embedding JavaScript in JSX

The true power of JSX is that you can break out of UI mode and inject JavaScript variables or math directly into the UI by using Curly Braces {}.
javascript
12345678910111213141516
import React from &#039;react&#039;;
import { View, Text } from &#039;react-native&#039;;

export default function UserProfile() {
  const username = "Alice";
  const points = 500;

  return (
    <View>
      {/* Injecting variables into text! */}
      <Text>Welcome back, {username}!</Text>
      {/* Injecting math logic into text! */}
      <Text>Score multiplied: {points * 2}</Text>
    </View>
  );
}

6. Functional Components

A component is just a JavaScript function that returns JSX. If you have a massive screen, you shouldn't write 500 lines of code in one function. You break it down into smaller functions.
javascript
12345678910111213141516171819
// 1. A small reusable component
const CustomButton = () => {
  return (
    <View style={{ backgroundColor: &#039;blue&#039;, padding: 10 }}>
      <Text style={{ color: &#039;white&#039; }}>Click Me!</Text>
    </View>
  );
};

// 2. The Main Screen Component
export default function App() {
  return (
    <View>
      <Text>Welcome to my App</Text>
      {/* Rendering the custom component! */}
      <CustomButton /> 
    </View>
  );
}

7. Component Composition

Component Composition is the art of assembling complex UI by snapping together smaller, simpler components.
javascript
1234567891011121314
const Header = () => <Text>My Dashboard</Text>;
const Footer = () => <Text>Copyright 2024</Text>;

export default function DashboardScreen() {
  return (
    <View>
      <Header />
      <View>
         <Text>Main Content goes here.</Text>
      </View>
      <Footer />
    </View>
  );
}

8. Visual Learning: The JSX Compilation

txt
1234567891011121314
[ You Write (JSX) ]
<View>
  <Text>Hello</Text>
</View>

       | (Babel Compiler translates it automatically)
       v

[ React Reads (JavaScript) ]
React.createElement(
  View,
  null,
  React.createElement(Text, null, &#039;Hello')
)

9. Common Mistakes

  • Returning an Object Instead of UI: If a function looks like this:
const MyText = () => { <Text>Hello</Text> } It will fail! Why? Because arrow functions with curly braces {} require an explicit return keyword. It must be return <Text>Hello</Text>; or implicitly returned using parentheses () => (<Text>Hello</Text>).

10. Best Practices

  • File Separation: For absolute beginners, writing 5 components in one file is fine. For professionals, every Component should get its own file (e.g., CustomButton.js) and be exported and imported where needed.

11. Practice Exercises

  1. 1. Fix this invalid JSX:
return (<Text>Top</Text> <Text>Bottom</Text>)
  1. 2. Write a Functional Component named Greeting that returns a <Text> containing the string "Hello World".

12. MCQs with Answers

Question 1

In JSX, how do you inject a dynamic JavaScript variable directly into the rendered text?

Question 2

Why will the following component crash the app?

13. Interview Questions

  • Q: Explain the role of Babel in a React Native project. Why is it strictly necessary when writing JSX?
  • Q: Describe Component Composition. Why is it architecturally superior to writing massive, 1000-line monolithic UI screens?
  • Q: What is the specific rule regarding sibling elements in JSX, and why does the React engine enforce this constraint?

14. FAQs

Q: Can I use if/else statements inside JSX? A: No, you cannot write a raw if/else block *inside* the JSX return statement. Instead, you must use JavaScript Ternary Operators (e.g., {isLogged ? <Text>Hi</Text> : <Text>Login</Text>}). We will cover this heavily in later chapters!

15. Summary

In Chapter 6, we unlocked the visual language of React Native: JSX. We learned that JSX is syntactic sugar that allows us to write UI structures comfortably while the compiler translates it to pure JavaScript. We memorized the golden rules of JSX: enforcing single parent wrappers, closing all tags, and utilizing camelCase properties. Finally, we learned how to architect our apps by building small, modular Functional Components and snapping them together via Component Composition.

16. Next Chapter Recommendation

Our components look great, but right now they are static and useless. We need to send dynamic data into them. Proceed to Chapter 7: Functional Components and Props.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·