Debugging React Native
Debugging React Native
🎯 Objective:
React Native apps throw red screen errors with stack traces that contain:
✅ Tip:
Use the error screen as a guide — don’t panic. Start from the top-most file/line and work down the
trace.
Start project:
npx expo start
Ask yourself:
1. What broke?
2. Where did it happen? (file/line/component)
3. Why did it break? (missing state, null value, etc.)
4. How can I confirm it? (console.log, component trace)
5. What’s the fix or workaround?
🎯 Goal:
To explore how React Native error messages work, how to interpret them, and how to debug your app
when things break.
Error Message:
Invalid prop 'source' supplied to 'Image'. Expected a number.
🔍 How to Interpret:
🔥 Important Insight: This is not intuitive unless you already know that
require('./path/to/image.png') returns a reference that React Native uses internally (as a
numeric ID) for bundled assets.
o Tells you:
The error happened in GoalInput.js
It was used by the App component
🧠 Why It Matters:
If you’re using GoalInput in multiple places, this helps you find which instance is causing the
error.
This narrows your search area for the bug.
📚 How to Fix It: Correct Way to Load an Image
Instead of using a plain string, use:
require() tells React Native to bundle and resolve the image during build time.
This ensures the app knows exactly where and how to find and load the image.
Go to: https://reactnative.dev/docs/image
Or search “React Native Image” on Google
⚠️Warnings vs Errors
Not all issues crash the app. Some are warnings:
🔎 Always click and read warnings — they often prevent future errors.
🖥️Where Errors Show Up
React Native displays errors in multiple places:
Location Description
📱 Emulator red screen Visible immediately if a fatal error occurs
🖥️Terminal (Expo CLI) Logs detailed errors, stack traces, and warnings
🧭 Expo DevTools in browser Also shows some runtime issues
🐛 In-app yellow/red boxes Warnings and exceptions shown directly in-app
"Where is the error? → Why is it happening? → What does the framework expect? → How
can I fix it?"
🔍 Debugging Without Errors: Understanding the App Flow
💡 Real-World Scenario:
Sometimes, your React Native app runs without any visible errors, but:
It behaves unexpectedly
Something doesn’t update
A state value isn’t what you expect
In these cases, using console.log() is a powerful and straightforward way to inspect the internal
behavior of your app.
🧰 What is console.log()?
A built-in JavaScript function that prints messages to the console (terminal).
Helps in:
o Debugging state values
o Tracking code execution
o Understanding component rendering
o Verifying function outputs
📍 Output Location:
➡️You’ll see the log in the terminal (where npm start or npx expo start was run).
You may see the log multiple times (once per instance).
This helps verify that each instance is running as expected.
📊 Use Cases for Logging
function GoalInput(props) {
console.log("GoalInput component rendered");
...
}
🧠 Expert Tip: Combine with useEffect() for even better lifecycle tracking.
function goalInputHandler(inputText) {
setEnteredGoalText(inputText);
console.log("Updated goal text:", inputText);
}
function addGoalHandler() {
if (enteredGoalText.trim().length === 0) {
console.log("Empty input. Goal not added.");
return;
}
While console.log() is great for basic checks, complex apps require deeper visibility:
npm start
or
You launch the Metro Bundler, which gives you access to useful shortcuts.
🔑 To View Shortcuts:
1. Click into the terminal window running Metro (make sure it’s active).
2. Press the ? (question mark) key.
3. You'll see a list of available hotkeys/shortcuts.
🚀 Common Shortcuts:
Shortcut Key Function
A Open Android Emulator
I Open iOS Simulator
R Reload the App
M Open Developer Menu (on Emulator)
Feature Description
Reload Manually refresh your app (like hot reload)
Go to Home Exit app and return to the Expo Launcher
Debug JS Remotely Open a debugging session in Chrome
Enable/Disable Fast Refresh Automatically reload app when code changes
Performance Monitor Show real-time performance info (FPS, JS threads)
🌐 Remote JavaScript Debugging (via Chrome)
✅ How to Enable:
Example:
console.log("GoalInput rendered");
Terminal
Chrome DevTools console (when remote debugging is active)
🧠 This is especially useful for inspecting backend calls, such as APIs, authentication, etc.
📝 Summary
Tool Purpose Access
Metro CLI Shortcuts Reload, open emulators ? key in terminal
Developer Menu Debug UI, JS, performance M key or emulator shortcut
Remote JS Debugging Full Chrome DevTools Developer Menu → Debug JS
Console Logs Print messages in app flow console.log()
🧠 Deep Debugging with React Developer Tools in React Native
In previous sections, we discussed basic debugging (console.log) and remote debugging using
Chrome DevTools. Now, let’s dive into a more advanced and powerful method: using React
Developer Tools with React Native.
💡 Unlike Chrome DevTools, React DevTools focus purely on React-specific debugging, making
them ideal for component-based inspection.
Keep your npm start / npx expo start process running in another terminal.
Step 2: Install DevTools globally
npm install -g react-devtools
This installs the standalone React DevTools CLI tool on your system.
react-devtools
Select:
✅ Debug Remote JS
✅ Now:
The DevTools will automatically connect to your running React Native app.
🧠 Note: Enabling remote debugging changes how the app is rendered (runs JS in a browser). This
might sometimes affect behavior like styles, especially background color issues.
Feature Description
Component Tree View the full nested tree of React components.
Inspect Components Click any component to see its props and state.
Live State Viewing See state updates in real-time as the app changes.
Manually change state values and instantly see how the app
Live State Editing
reacts.
Inspect Props Check what data is passed to each component.
Understand Component Helps in debugging conditional rendering, nested components,
Structure etc.
🧪 Example:
📝 Summary Table
Tool Purpose Access
React DevTools Inspect React components, props, Install with npm install -g
Tool Purpose Access
(Standalone) state react-devtools
Enable Remote Debugging to
Developer Menu Emulator or press M in terminal
connect DevTools
Chrome DevTools Debug JS, network, console logs Opened via "Debug Remote JS"