React Native lets you build mobile apps that work on both iPhone and Android using mostly the same code. This saves time and effort.
Why React Native enables cross-platform mobile apps
import React from 'react'; import { View, Text } from 'react-native'; export default function App() { return ( <View> <Text>Hello, React Native!</Text> </View> ); }
This is a simple React Native component that works on both iOS and Android.
React Native uses native UI components under the hood, so the app feels like a real native app.
import { Button, Alert } from 'react-native'; <Button title="Click me" onPress={() => Alert.alert('Clicked!')} />
import { Platform, Text } from 'react-native'; <Text>{Platform.OS === 'ios' ? 'Running on iOS' : 'Running on Android'}</Text>
This app shows a welcome message and tells the user if they are on iPhone or Android. It uses React Native components and styles that work on both platforms.
import React from 'react'; import { View, Text, StyleSheet, Platform } from 'react-native'; export default function App() { return ( <View style={styles.container}> <Text style={styles.text}>Welcome to React Native!</Text> <Text style={styles.platformText}> {Platform.OS === 'ios' ? 'You are on iPhone' : 'You are on Android'} </Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0' }, text: { fontSize: 24, fontWeight: 'bold', marginBottom: 10 }, platformText: { fontSize: 18, color: '#555' } });
React Native uses JavaScript and React to build mobile apps that feel native.
You can write most of your app once and run it on both iOS and Android.
Some platform-specific code may be needed for special features or styles.
React Native enables cross-platform apps by using shared JavaScript code and native UI components.
This approach saves time and effort compared to building separate apps for iOS and Android.
You can still customize parts of your app for each platform when needed.
Practice
Solution
Step 1: Understand React Native's code sharing
React Native uses JavaScript to write most of the app logic once, which works on both iOS and Android.Step 2: Recognize native UI rendering
It renders native UI components on each platform, so the app looks and feels native.Final Answer:
Because it uses JavaScript to write shared code and renders native UI components on each platform -> Option DQuick Check:
Shared JavaScript + native UI = cross-platform [OK]
- Thinking React Native runs apps inside a web browser
- Believing it compiles JS directly to machine code
- Assuming separate code is needed for each platform
Solution
Step 1: Identify the official React Native import syntax
The correct way is to import components like View and Text from the 'react-native' package.Step 2: Eliminate incorrect imports
Imports from wrong package names like 'react-native-view' (separate imports), 'react', or 'react-native-web' are not standard.Final Answer:
import { View, Text } from 'react-native'; -> Option BQuick Check:
Standard React Native imports use 'react-native' [OK]
- Importing from 'react' instead of 'react-native'
- Using non-existent packages like 'react-native-view'
- Confusing React Native with React Native Web imports
import React from 'react';
import { Text, View } from 'react-native';
export default function App() {
return (
Hello, cross-platform!
);
}Solution
Step 1: Analyze the JSX structure
The code returns a View containing a Text component with the string 'Hello, cross-platform!'.Step 2: Understand React Native rendering
View and Text are native components that render properly on both iOS and Android.Final Answer:
A native app screen showing the text 'Hello, cross-platform!' on both iOS and Android -> Option CQuick Check:
View + Text render native UI with text [OK]
- Thinking View and Text cause errors when used together
- Assuming the screen will be blank without styles
- Confusing React Native with web rendering
import React from 'react';
import { Button } from 'react-native';
export default function App() {
return (
);
}Solution
Step 1: Check the Button component usage
The title prop value should be a string, so it must be enclosed in quotes.Step 2: Identify the syntax error
title=Click Me is invalid; it should be title="Click Me" or title='Click Me'.Final Answer:
The title prop value is missing quotes, causing a syntax error -> Option AQuick Check:
Props with string values need quotes [OK]
- Omitting quotes around string props
- Thinking Button needs a View wrapper to work
- Believing Button is not a valid React Native component
Solution
Step 1: Understand React Native's Platform module
The Platform module lets you detect the OS and apply platform-specific code or styles within the same app.Step 2: Apply conditional rendering for customization
You can write shared code but customize UI parts like buttons differently for iOS and Android using Platform checks.Final Answer:
Use Platform module to conditionally render different styles or components for each platform -> Option AQuick Check:
Platform module enables platform-specific customization [OK]
- Thinking you must write separate apps for each platform
- Assuming React Native can't customize UI per platform
- Confusing React Native with web-only solutions
Ready to prove it?
Pass the test and this pattern moves unknown
