React Training
  • React JS Library
  • Roadmap
  • Training OutLine
  • React js basics
    • Understanding React JS
    • React JS a framework?
    • Setting Up React
    • Say Hello to React
    • Everything is a Component
    • Create-react-app
  • React Building Blocks
    • JSX and Babel
    • One Way Data Flow
    • Virtual DOM
    • V of MVC
    • React Terminology
    • React Tooling
  • Day 01
    • Day 01 OutLine
    • All About JSX
    • React Tools/ npm & webpack
    • Introduction of Webpack
    • Hello world using webpack
      • Webpack Setting up with React
    • NPM Scripts | Package JSON
      • Package.json file
    • React JS Principles
      • One Way Data Flow
      • V of MVC
      • Virtual DOM
    • Create React App - Part-1
    • Create React App - Part-2
  • Day 02
    • Quick Recap
      • Quiz
    • State & Props
      • State & Props in Depth
      • State Vs Props | Compare
    • React LifeCycle Methods
      • React LifeCycle Methods for V-0.16 x
    • Constructor | Lifecycle
    • Write Flicker App | First App
  • Day 03
    • Quick Recap
    • Life Cycle Flow
      • Birth and Mounting
      • Initlization and Construction
      • Pre Mounting
      • Render Method
      • componentDidMount
    • Type of React Components
      • Examples- Quick Compare
      • Class and Functional components
      • Functional Components
    • YouTube application
      • Component Design
    • All in One LifeCycle
  • Assignment
    • React App development
  • Day 04
    • Quick Recap on Lifecycle
    • Lifecycle deprecated/New Methods
      • New Lifecycle Methods
    • Lets Build App Netflix | Mock
  • Assignment
    • Github battle App | Assignment
  • Day 05
    • Quick Recap : Hooks
    • ES6 Features | Hands-on
      • ES6 Code Examples
    • Next Stop - React Router | SPA
      • Code examples | Router
      • React Router Building Blocks
      • Application using react-router-dom
  • Day 06
    • Router V4 | Quick Recap
    • ES2015 | 16 Quick Recap
    • LifeCycle Methods -Part-1
    • LifeCycle Methods -Part-2
  • Day 07
    • Quick Recap | New Lifecycle
    • Quick Recap | React Routing
    • Context API | React JS
      • component with context APIs
      • Context API | examples
    • App using Hooks/Context APIs
  • Assignment
    • Assignments
  • State Management Day-08
    • Quick Recap
    • Managing React State
      • What is Redux
      • Understanding Redux
      • Hello World "Redux"
  • React Redux Day - 09
    • Redux State Manager
    • Redux Redux Development
    • Simple Application | Redux
  • Redux Live Application Day -10
    • Redux with existing Application
      • Redux with React App
      • Lets Build More Apps
      • Should I use Redux from Dan
    • Quick Look at JS in React
    • Learn By Reading
  • miscellaneous Items - Day 11
    • Hooks useReducer
    • Hooks useContext
    • Hooks useRef
    • Hooks useEffect
    • Hooks useState
    • Lazy Loading and code splitting
    • Styling React Component
  • React Next Step - Day 12
    • Topics
    • Jest and Enjyme Testing
    • Examples: Testing
  • React Native
    • What is React Native
    • Setting up Environment
      • Linux Systems
    • React Native Hello World App
    • React Native Architecture
    • React Native Vs Native
    • Expo Cli or React Native CLI
  • React Native core Fundamental
    • React Native "Hello World"
    • Course OutLine
    • Getting started with Expo App
    • Layout with Flexbox
    • Working with Styles and Events
    • Manging Component State and Props
    • Build Simple Task List Application
  • What to Debug & How to Debug
    • Debug React Native Application
Powered by GitBook
On this page
  • Debugging
  • Reactotron
  • Inspecting
  • Touchables
  • Spying on the Bridge
  • Slowlog
  • JSEventLoopWatchdog
  • React Perf Tools

Was this helpful?

  1. What to Debug & How to Debug

Debug React Native Application

PreviousBuild Simple Task List Application

Last updated 4 years ago

Was this helpful?

When doing application development with interactivity, UI/UX and state, on a resource-limited platform, these questions often come up:

  • Debugging — how do you debug applications?

  • Inspecting — how do you inspect the UI tree?

  • Touchables — how to understand interactive surfaces?

  • Platform — how to reason about the “magical things” that happen out of sight?

  • Slowness — how to get visibility about what’s slow?

  • Layout — Specifically, about React — how to reason about React performance and efficiency?

This is an overview of what’s available to you within React Native and its ecosystem.

Debugging

Debugging is seamless. You can debug Javascript, step into native, back to Javascript without configuring anything, and relying on pointed at a that belongs to React Native.

  1. Cmd+Z (or shake) to bring up the dev menu.

  2. Select ‘Debug JS Remotely’.

  3. A fresh Chrome instance will pop up, which you can immediately use for setting breakpoints and stepping through your app’s Javascript code.

Image for post

Remember to turn off remote debugging when done, by accessing the developer menu again (Cmd+Z).

Even if React Native doesn’t support the more advanced time, group, table interface, it would pop a yellowbox on warn, for example. React Native promotes a very fast feedback cycle, and using console.log rhymes better with that, rather than stop-the-world debugging and having to context switch — getting out of the editor and using a completely different toolset.

NOTE: There’s also Systrace, but given that it is quite low-level, we’ll cover in its own article.

Reactotron

Reactotron plugs into your Redux pipeline as a middleware and lets you inspect, fiddle and trace it with a live ncurses based interface which is really fun to use.

Inspecting

  1. Cmd+Z (or shake) to bring up dev menu.

  2. Select Inspector.

  3. Tap on ‘Inspect’.

You can now proceed to select a component, drill down or up the UI tree hierarchy by tapping the breadcrumbs pieces, and verify positioning, margins, padding and more. You can use this to find overlapping areas, surfaces that block event bubbling, transparent surfaces that overflow the existing viewport, and more.

Touchables

Showing areas that are touchable (users can tap those) can be useful when the interface is seamless and does not expose a clear border region for each touchable.

You can expose these with the inspector as well:

  1. Cmd+Z (or shake) to bring up dev menu.

  2. Select Inspector.

  3. Tap on ‘Touchables’.

You can then hide the inspector and continue interacting with your application while every touchable is exposed. Crushed border line, color depicts type of touchable:

Spying on the Bridge

In a very high-level view, the architecture of React Native is divided into user code and platform code. User code, is typically your application Javascript code, and platform code is typically a mixture of Native and Javascript code as part of React Native. These two parts communicate over the React Native bridge.

There’s a lot to gain by simply looking at these communications. You could find bottlenecks (by identifying too much chatter over the wire), resource hogs (by observing very large objects passing back and forth), bugs (by finding misplaced communication, forgotten timers, and so on) and more.

Here are some ideas for things you can observe over the bridge with Snoopy:

  • Buffer events for one second, and raise an alert when the total events cross a threshold.

  • Identify huge calls by looking at the arguments for each event.

  • Keep an eye open by charting out the bridge communication volumes.

Slowlog

I wanted to have this same concept for React Native applications.

JSEventLoopWatchdog

Here’s a neat idea: if you set a Javascript timer to fire every second, and on every tick you look at the wall clock to make sure that exactly one second has passed, then you can detect if your Javascript code is running late.

This idea is exactly what the JSEventLoopWatchdog component in React Native core does. It Detects stalls in the React Native Javascript event loop by scheduling a no-op task every X amount of time, recording the wall clock time it executed at in reality, and compares it to the expected time. If we have a stall, there will be a difference in clock times. The bigger the stall the bigger the difference.

To use JSEventLoopWatchdog, you supply a threshold time — the duration, in milliseconds, equal to the lag you’re willing to accept. When the lag is bigger, you’ll get console messages notifying you about it:

const JSEventLoopWatchdog = require('JSEventLoopWatchdog');
JSEventLoopWatchdog.install({thresholdMS: 200});

To use JSEventLoopWatchdog on your own, you need to use the watchdog component of slowlog:

import watchdog from 'react-native-slowlog/watchdog'
watchdog.install({thresholdMS: 50})

Now, if anything is stalled for more than 50ms, you’ll get a notification in your logs.

React Perf Tools

Last but not least, remember you still have all the vanilla React tooling available. The official docs makes a better explanation than I would ever make here, so I’ll just point you to it:

Using console.log, or “console.log debugging” is also very common. It might seem a bit primitive, however with time console.log became a rather for the Javascript developer, and it definitely isn’t equal to a dirty-feeling puts/print/cout/what-have-you.

If you haven’t already, go ahead and try now. If you’re like me, you can’t get enough of command-line tools, and this one is one of the best you can use around React and Redux.

Image for post

Much like in Chrome’s when inspecting the DOM for regular web pages, you can enable live inspection of React Native components.

Image for post
Image for post
Image for post

React Native comes out of the box with a special SPY_MODE flag that you can turn on in its MessageQueue component; through which these kind of communications pass. However, this was not exposed to end-users, but for React Native developers. I wanted to change that with , by allowing end users to plug in their own “spies”.

I then proceeded to build a spying framework which you can use today called .

And here’s how it looks like:

Aggregating and Charting Events with Bar

With Snoopy, you can build sophisticated bridge event pipelines using the powerful and concepts that declaratively model what you want to observe and detect, instead of how you want to detect it.

Check out the for more.

Update: I published an about using Snoopy!

Many databases implement a feature called a “” or simply “slowlog”, which is a file that the database uses to log very slow queries. An administrator can simply tail this file, and/or ship it to a log aggregation service that notifies her when a slow query happens.

With , you wire such a capability into your React components piecemeal, and get a high-level idea in real time of the slow things that happen around your app. It is not a fine-grained diagnostics tool, but rather a general watchdog that sits there and pops an alert when things are majorly slow.

You can find out more about slowlog in the .

The bad news is that this is still an internal tool React Native uses since it’s not currently usable if you’re out of the react-native project space. The good news is that I’ve extracted it into and adapted the internals so that it can run outside of the react-native core.

powerful tool
Reactotron
devtools
this pull request
Snoopy
Reactive Extensions
FRP
Snoopy repository
in-depth article
Slow Query Log
react-native-slowlog
react-native-slowlog repository
react-native-slowlog
Chrome devtools
debug interface
Aggregating and Charting Events with Bar