JSX and Components in React Native
# 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 (likeReact.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.
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 />.
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{}.
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.7. Component Composition
Component Composition is the art of assembling complex UI by snapping together smaller, simpler components.8. Visual Learning: The JSX Compilation
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 beexported andimported where needed.
11. Practice Exercises
- 1. Fix this invalid JSX:
return (<Text>Top</Text> <Text>Bottom</Text>)
-
2.
Write a Functional Component named
Greetingthat returns a<Text>containing the string "Hello World".
12. MCQs with Answers
In JSX, how do you inject a dynamic JavaScript variable directly into the rendered text?
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 useif/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!