This React Native component fetches data from a web address using the Fetch API with a GET request. It shows the data on the screen or a loading message while waiting.
Widget Tree
View
├─ Text (title)
├─ Button (Fetch Data)
└─ Text (data or loading message)
The main container is a View. Inside it, a Text shows a title. Below is a Button that triggers the data fetch. Finally, another Text shows the fetched data or a loading message.
Render Trace - 4 Steps
Step 1: View
Step 2: Text (title)
Step 3: Button
Step 4: Text (data or loading)
State Change - Re-render
Trigger:User presses the 'Fetch Data' button
Before
data is empty string, loading is false
→
After
loading is true, data is empty string; then loading is false, data is set to fetched text
Re-renders:The entire component re-renders to update the loading message and then show fetched data
UI Quiz - 3 Questions
Test your understanding
What happens on the screen immediately after pressing the 'Fetch Data' button?
AThe text changes to 'Loading...'
BThe button disappears
CThe title text changes
DNothing changes
Key Insight
Using the Fetch API in React Native involves triggering a network request on user action, managing loading state, and updating the UI to show feedback. The component re-renders when state changes to reflect loading and data results, providing a smooth user experience.
Practice
(1/5)
1.
What does the Fetch API do in React Native?
Choose the best description.
easy
A. It retrieves data from a web server using HTTP requests.
B. It styles components with CSS.
C. It manages app navigation between screens.
D. It stores data locally on the device.
Solution
Step 1: Understand Fetch API purpose
The Fetch API is designed to make network requests to get or send data.
Step 2: Match description to Fetch API
Only It retrieves data from a web server using HTTP requests. describes retrieving data from a web server, which is what Fetch does.
Final Answer:
It retrieves data from a web server using HTTP requests. -> Option A
Quick Check:
Fetch API = Data retrieval [OK]
Hint: Fetch is for network data requests, not styling or storage [OK]
Common Mistakes:
Confusing Fetch with styling or navigation libraries
Thinking Fetch stores data locally
Assuming Fetch only works with JSON
2.
Which of the following is the correct syntax to fetch JSON data from https://api.example.com/data?
fetch(_____).then(response => response.json())
easy
A. "https://api.example.com/data"
B. url
C. https://api.example.com/data
D. 'https://api.example.com/data'
Solution
Step 1: Identify correct string syntax in JavaScript
URLs must be passed as strings, enclosed in quotes.
Step 2: Choose proper quotes for URL string
'https://api.example.com/data' uses single quotes correctly.
Final Answer:
'https://api.example.com/data' -> Option D
Quick Check:
URL as string = 'https://api.example.com/data' [OK]
Hint: Always wrap URLs in quotes for fetch [OK]
Common Mistakes:
Not using quotes around the URL
Using undefined variable url without declaration
Using double quotes inside double quotes without escaping
3.
What will be logged to the console after running this code?