React Native, from a blank machine to a shipped app · Chapter 3 of 12 · Mobile · new chapter every Monday morning
By the end of this chapter: Render something real, change it, and understand exactly why the screen updated.
The problem
Emma, a junior mobile engineer, has just finished the "Hello World" screen of her first React Native project. She wants to add a feature that parses a CSV file on the device and displays a progress bar while the work is being done. To achieve this, she writes a JavaScript for loop that iterates over the rows, updates a state variable each 100 rows, and expects the UI to stay responsive. However, when she runs the app, the progress bar never moves, the app freezes for several seconds, and the iOS build logs a warning. Emma is unsure why her app is not updating the UI as expected and is looking for a solution to fix this issue.
The symptom Emma sees is that the app freezes, and the progress bar does not update. This costs her time, as she is unable to deliver the feature as expected, and potentially affects the user experience. The warning in the iOS build logs suggests that there might be an issue with the way the JavaScript code is interacting with the native threads or the renderer.
Emma's situation is a common one in React Native development, where the JavaScript thread can block the native threads, causing the app to freeze. To fix this issue, Emma needs to understand how React Native components, props, and state work together to update the UI.
Before you start
To follow along with this chapter, you should have a React Native project set up with the latest version of React Native (0.76) and have completed the previous chapters in this series. You should also have Node.js (16.14.2) and JDK (11) installed on your machine. To verify that your setup is correct, run the following command in your terminal:
npx react-native --version This should output the version of React Native you are using. If you are using an older version, update to the latest version using:
npx react-native upgrade Make sure you have the necessary dependencies installed by running:
npm install or
yarn install depending on your package manager.
Understanding components, props, and state
To fix Emma's issue, we need to understand how React Native components, props, and state work together to update the UI. A React Native component is a JavaScript function that returns a JSX element. Props are short for "properties" and are used to pass data from a parent component to a child component. State, on the other hand, is used to store data that changes over time.
In Emma's case, the for loop is blocking the JavaScript thread, which is preventing the UI from updating. To fix this, we need to use a different approach that allows the JavaScript thread to yield control back to the native threads.
Using setState to update the UI
To update the UI in React Native, we need to use the setState method. setState is a method that updates the state of a component and triggers a re-render of the component. When we call setState, React Native updates the state and then re-renders the component with the new state.
However, in Emma's case, the for loop is blocking the JavaScript thread, which is preventing the setState method from updating the UI. To fix this, we can use the setTimeout function to yield control back to the native threads.
Build it
To fix Emma's issue, we can use the following steps:
- Create a new component that displays the progress bar.
- Use the
setStatemethod to update the progress bar. - Use the
setTimeoutfunction to yield control back to the native threads.
Here is an example of how we can implement this:
import React, { useState, useEffect } from 'react'; import { View, Text, ProgressBarAndroid } from 'react-native'; const ProgressBar = () => { const [progress, setProgress] = useState(0); useEffect(() => { const intervalId = setInterval(() => { setProgress((prevProgress) => { if (prevProgress >= 100) { clearInterval(intervalId); return 100; } return prevProgress + 10; }); }, 1000); return () => clearInterval(intervalId); }, []); return ( <View> <Text>Progress: {progress}%</Text> <ProgressBarAndroid styleAttr="Horizontal" progress={progress} /> </View> ); }; export default ProgressBar; In this example, we create a new component that displays a progress bar. We use the useState hook to store the progress state, and the useEffect hook to update the progress state every second. We use the setInterval function to schedule the updates, and the clearInterval function to cancel the updates when the progress reaches 100%.
When this breaks
There are several ways this code can break. For example:
- If the
setIntervalfunction is not cleared properly, it can cause a memory leak. - If the
setStatemethod is not used correctly, it can cause the UI to not update properly. - If the
useEffecthook is not used correctly, it can cause the component to not re-render properly.
To fix these issues, we need to make sure to clear the setInterval function properly, use the setState method correctly, and use the useEffect hook correctly.
What it costs
Using setState and setTimeout to update the UI has some costs. For example:
- It can cause the UI to update slowly, especially if the updates are frequent.
- It can cause the JavaScript thread to block, especially if the updates are complex.
- It can cause the native threads to block, especially if the updates are not yielded properly.
To mitigate these costs, we need to make sure to use setState and setTimeout judiciously, and to yield control back to the native threads regularly.
In the interview
In an interview, you may be asked to explain how React Native components, props, and state work together to update the UI. You may be asked to write code that updates a progress bar, and to explain how the code works.
A weak answer might be: "I use setState to update the UI, and it just works."
A strong answer would be: "I use setState to update the UI, but I also need to make sure to yield control back to the native threads using setTimeout. This is because the JavaScript thread can block the native threads, causing the UI to not update properly. I also need to make sure to clear the setInterval function properly to avoid memory leaks."
Your tasks
- Create a new React Native project and add a progress bar component that updates every second.
- Use the
setStatemethod to update the progress bar, and thesetTimeoutfunction to yield control back to the native threads. - Add a button that starts and stops the progress bar.
- Use the
useEffecthook to update the progress bar when the component mounts and unmounts. - Test the progress bar on a real device to make sure it updates properly.
When you have completed these tasks, you should have a working progress bar component that updates every second, and you should understand how React Native components, props, and state work together to update the UI.
Your tasks this week
Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.
Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.
React Native, from a blank machine to a shipped app
Chapter 3 of 12. New chapter every Monday morning.
Next: Layout on a phone: the Flexbox rules that cover almost everything.
· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe
Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.
Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.