What if you could build your first mobile app that works everywhere with just one simple code?
Why First React Native app? - Purpose & Use Cases
Imagine you want to create a simple mobile app that shows a welcome message on your phone screen. Without React Native, you'd have to write separate code for Android and iOS, learning two different languages and tools.
This means more time, more mistakes, and frustration. You might spend days just setting up the environment and fixing errors instead of building your app. It feels like juggling two different puzzles at once.
React Native lets you write one code using JavaScript that works on both Android and iOS. It handles the hard parts behind the scenes, so you can focus on making your app look and work great.
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } // And a separate Swift file for iOS
import React from 'react'; import { Text, View } from 'react-native'; export default function App() { return ( <View> <Text>Welcome to React Native!</Text> </View> ); }
You can quickly build apps that run on many devices with one simple codebase, saving time and effort.
Think about a small business owner who wants an app for customers on both Android and iPhone. With React Native, they can launch faster without hiring two developers.
Writing separate apps for Android and iOS is slow and hard.
React Native lets you write one app for both platforms.
This saves time and makes app building easier and more fun.
Practice
View component in a React Native app?Solution
Step 1: Understand the role of
TheViewViewcomponent is like a box that holds other components and helps arrange them on the screen.Step 2: Compare with other components
Textdisplays text, butViewis for layout and grouping.Final Answer:
To act as a container for other components and layout -> Option AQuick Check:
View= container [OK]
- Confusing
ViewwithText - Thinking
Viewhandles user input - Assuming
Viewfetches data
Solution
Step 1: Check React import
React is imported as default from 'react', soimport React from 'react';is correct.Step 2: Check React Native components import
Components likeViewandTextare named exports from 'react-native', soimport { View, Text } from 'react-native';is correct.Final Answer:
import React from 'react'; import { View, Text } from 'react-native'; -> Option AQuick Check:
Default React + named components import [OK]
- Importing React components from 'react-native' incorrectly
- Using default import for View and Text
- Missing curly braces for named imports
import React from 'react';
import { View, Text } from 'react-native';
export default function App() {
return (
Hello, React Native!
);
}Solution
Step 1: Analyze the returned JSX
TheAppfunction returns aViewcontaining aTextcomponent with the string 'Hello, React Native!'.Step 2: Understand rendering behavior
React Native will display the text inside theTextcomponent on the screen inside theView.Final Answer:
The text 'Hello, React Native!' displayed on the screen -> Option CQuick Check:
Text inside View renders as visible text [OK]
- Thinking style is required to show text
- Confusing View with Button
- Expecting an error without styles
import React from 'react';
import { View, Text } from 'react-native';
export default function App() {
return (
Hello World
)
}Solution
Step 1: Check JSX syntax
The JSX inside the return is wrapped in parentheses, which is optional but allowed. The code uses valid JSX syntax.Step 2: Check React Native rules
Viewdoes not require a style prop to work. Semicolons are optional in JavaScript.Final Answer:
No error; the code is correct and will run -> Option BQuick Check:
Valid JSX and React Native code [OK]
- Thinking style prop is mandatory
- Expecting semicolons are required
- Assuming parentheses are always needed
View component to achieve this?Solution
Step 1: Understand centering in React Native
To center content vertically and horizontally, the container needs to fill the screen withflex: 1and usejustifyContent: 'center'for vertical centering andalignItems: 'center'for horizontal centering.Step 2: Evaluate each style option
{ flex: 1, justifyContent: 'center', alignItems: 'center' } uses the correct flexbox properties for centering. Options A, B, and D do not center content both ways.Final Answer:
{ flex: 1, justifyContent: 'center', alignItems: 'center' } -> Option DQuick Check:
Flexbox center with flex:1 [OK]
- Using padding or margin to center content
- Forgetting flex:1 to fill screen
- Using wrong flexDirection or positioning
Ready to prove it?
Pass the test and this pattern moves unknown
