Bird
Raised Fist0
React Nativemobile~20 mins

Profiling with Flipper in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Performance Profiler Setup
This screen helps you set up Flipper profiling in a React Native app to monitor performance and debug UI issues.
Target UI
┌───────────────────────────────┐
│ Performance Profiler Setup     │
├───────────────────────────────┤
│ [ ] Enable Flipper Profiling   │
│                               │
│ [Start Profiling] [Stop]       │
│                               │
│ Logs:                         │
│ ----------------------------- │
│                               │
│                               │
└───────────────────────────────┘
Add a toggle switch to enable or disable Flipper profiling.
Add two buttons: 'Start Profiling' and 'Stop' to control profiling state.
Display a simple log area that shows profiling status messages.
Use React Native components and hooks to manage state.
Ensure the UI is accessible with proper labels.
Starter Code
React Native
import React, { useState } from 'react';
import { View, Text, Switch, Button, ScrollView, StyleSheet } from 'react-native';

export default function ProfilerSetup() {
  const [isEnabled, setIsEnabled] = useState(false);
  const [logs, setLogs] = useState([]);

  // TODO: Add handlers for toggling and buttons

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Performance Profiler Setup</Text>
      <View style={styles.row}>
        <Switch
          value={isEnabled}
          onValueChange={(value) => {
            setIsEnabled(value);
            // TODO: Add log update
          }}
          accessibilityLabel="Enable Flipper Profiling"
        />
        <Text style={styles.label}>Enable Flipper Profiling</Text>
      </View>
      <View style={styles.buttonRow}>
        <Button title="Start Profiling" onPress={() => { /* TODO */ }} />
        <Button title="Stop" onPress={() => { /* TODO */ }} />
      </View>
      <Text style={styles.logTitle}>Logs:</Text>
      <ScrollView style={styles.logBox}>
        {/* TODO: Render logs here */}
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, padding: 20, backgroundColor: '#fff' },
  title: { fontSize: 20, fontWeight: 'bold', marginBottom: 20 },
  row: { flexDirection: 'row', alignItems: 'center', marginBottom: 20 },
  label: { marginLeft: 10, fontSize: 16 },
  buttonRow: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 20 },
  logTitle: { fontWeight: 'bold', fontSize: 16, marginBottom: 10 },
  logBox: { borderWidth: 1, borderColor: '#ccc', height: 150, padding: 10, backgroundColor: '#f9f9f9' }
});
Task 1
Task 2
Task 3
Solution
React Native
import React, { useState } from 'react';
import { View, Text, Switch, Button, ScrollView, StyleSheet } from 'react-native';

export default function ProfilerSetup() {
  const [isEnabled, setIsEnabled] = useState(false);
  const [logs, setLogs] = useState([]);

  const addLog = (message) => {
    setLogs((prevLogs) => [message, ...prevLogs]);
  };

  const toggleSwitch = (value) => {
    setIsEnabled(value);
    addLog(`Profiling ${value ? 'enabled' : 'disabled'}`);
  };

  const startProfiling = () => {
    if (isEnabled) {
      addLog('Profiling started');
    } else {
      addLog('Enable profiling first');
    }
  };

  const stopProfiling = () => {
    if (isEnabled) {
      addLog('Profiling stopped');
    } else {
      addLog('Profiling is not enabled');
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Performance Profiler Setup</Text>
      <View style={styles.row}>
        <Switch
          value={isEnabled}
          onValueChange={toggleSwitch}
          accessibilityLabel="Enable Flipper Profiling"
        />
        <Text style={styles.label}>Enable Flipper Profiling</Text>
      </View>
      <View style={styles.buttonRow}>
        <Button title="Start Profiling" onPress={startProfiling} accessibilityLabel="Start Profiling button" />
        <Button title="Stop" onPress={stopProfiling} accessibilityLabel="Stop Profiling button" />
      </View>
      <Text style={styles.logTitle}>Logs:</Text>
      <ScrollView style={styles.logBox} accessibilityLabel="Profiling logs">
        {logs.length === 0 ? (
          <Text>No logs yet</Text>
        ) : (
          logs.map((log, index) => (
            <Text key={index} style={styles.logText}>{log}</Text>
          ))
        )}
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, padding: 20, backgroundColor: '#fff' },
  title: { fontSize: 20, fontWeight: 'bold', marginBottom: 20 },
  row: { flexDirection: 'row', alignItems: 'center', marginBottom: 20 },
  label: { marginLeft: 10, fontSize: 16 },
  buttonRow: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 20 },
  logTitle: { fontWeight: 'bold', fontSize: 16, marginBottom: 10 },
  logBox: { borderWidth: 1, borderColor: '#ccc', height: 150, padding: 10, backgroundColor: '#f9f9f9' },
  logText: { fontSize: 14, marginBottom: 4 }
});

We use React Native's useState hook to keep track of whether profiling is enabled and to store log messages.

The toggleSwitch function updates the enabled state and adds a log message about the change.

The startProfiling and stopProfiling functions check if profiling is enabled before adding appropriate log messages.

The logs are displayed in a scrollable area, showing the most recent messages at the top.

Accessibility labels are added to controls for screen reader support.

Final Result
Completed Screen
┌───────────────────────────────┐
│ Performance Profiler Setup     │
├───────────────────────────────┤
│ [x] Enable Flipper Profiling   │
│                               │
│ [Start Profiling] [Stop]       │
│                               │
│ Logs:                         │
│ ----------------------------- │
│ Profiling started             │
│ Profiling enabled             │
│                               │
└───────────────────────────────┘
Tapping the switch toggles profiling on or off and logs the change.
Pressing 'Start Profiling' logs 'Profiling started' if enabled, otherwise prompts to enable first.
Pressing 'Stop' logs 'Profiling stopped' if enabled, otherwise informs profiling is not enabled.
Logs appear in the scrollable area with newest messages on top.
Stretch Goal
Add a button to clear all logs from the log area.
💡 Hint
Create a 'Clear Logs' button that sets the logs state to an empty array when pressed.

Practice

(1/5)
1. What is the main purpose of using Flipper with a React Native app?
easy
A. To write new React Native components
B. To deploy the app to app stores
C. To profile and debug app performance issues
D. To create app UI layouts visually

Solution

  1. Step 1: Understand Flipper's role

    Flipper is a tool designed to connect with React Native apps to help developers see performance data and debug issues.
  2. Step 2: Identify the main use case

    It is not used for writing code, deploying apps, or designing UI, but for profiling and debugging.
  3. Final Answer:

    To profile and debug app performance issues -> Option C
  4. Quick Check:

    Profiling with Flipper = Debugging performance [OK]
Hint: Flipper is for debugging and profiling, not coding or deploying [OK]
Common Mistakes:
  • Thinking Flipper is a code editor
  • Confusing Flipper with deployment tools
  • Assuming Flipper designs UI
2. Which of the following is the correct way to start Flipper for profiling a React Native app?
easy
A. Run the command flipper start in the terminal
B. Open the Flipper desktop app and connect your device
C. Add import Flipper from 'flipper' in your app code
D. Use npm install flipper to install Flipper in the app

Solution

  1. Step 1: Know how Flipper runs

    Flipper is a desktop app you open to connect to your running React Native app for profiling.
  2. Step 2: Identify the correct start method

    You do not start Flipper via terminal commands or import it in app code; you open the desktop app and connect your device.
  3. Final Answer:

    Open the Flipper desktop app and connect your device -> Option B
  4. Quick Check:

    Flipper runs as desktop app = Open the Flipper desktop app and connect your device [OK]
Hint: Flipper is a desktop app you open, not a command or package [OK]
Common Mistakes:
  • Trying to start Flipper via terminal command
  • Importing Flipper in app code incorrectly
  • Installing Flipper as an npm package
3. Given you have connected Flipper to your React Native app, which plugin would you use to check memory usage over time?
medium
A. Memory plugin
B. Network plugin
C. React DevTools plugin
D. Layout plugin

Solution

  1. Step 1: Understand Flipper plugins

    Flipper has plugins for different profiling tasks: React DevTools for component tree, Network for requests, Memory for memory usage, Layout for UI layout.
  2. Step 2: Match plugin to task

    To check memory usage over time, the Memory plugin is the correct choice.
  3. Final Answer:

    Memory plugin -> Option A
  4. Quick Check:

    Memory usage = Memory plugin [OK]
Hint: Memory plugin shows memory data, not Network or Layout [OK]
Common Mistakes:
  • Choosing Network plugin for memory data
  • Using React DevTools for memory profiling
  • Confusing Layout plugin with memory
4. You notice Flipper is not showing network requests from your React Native app. What is a common cause and fix?
medium
A. You forgot to enable the Flipper Network plugin in the app
B. You need to reinstall Flipper desktop app
C. Flipper only works on iOS, switch to iOS device
D. The app is not running; start the app first

Solution

  1. Step 1: Identify common causes for missing network data

    Flipper requires the Network plugin enabled in the app to capture requests.
  2. Step 2: Choose the fix

    If network requests don't show, enabling the Network plugin in the app code or config usually fixes it.
  3. Final Answer:

    You forgot to enable the Flipper Network plugin in the app -> Option A
  4. Quick Check:

    Enable Network plugin = show requests [OK]
Hint: Enable Network plugin in app to see requests in Flipper [OK]
Common Mistakes:
  • Assuming Flipper only works on iOS
  • Restarting Flipper without checking plugin settings
  • Not running the app at all
5. You want to profile CPU usage spikes in your React Native app using Flipper. Which steps should you follow to capture and analyze this data effectively?
hard
A. Use Flipper's Layout plugin to check CPU usage visually
B. Install a CPU monitoring npm package and ignore Flipper
C. Restart the app multiple times without Flipper to clear CPU spikes
D. Open Flipper, enable the CPU Profiler plugin, run the app workload, then review the CPU timeline

Solution

  1. Step 1: Use Flipper's CPU Profiler plugin

    Flipper provides a CPU Profiler plugin to record CPU usage over time during app execution.
  2. Step 2: Run app workload and analyze timeline

    Enable the plugin, run the app workload causing spikes, then review the CPU timeline in Flipper to find issues.
  3. Final Answer:

    Open Flipper, enable the CPU Profiler plugin, run the app workload, then review the CPU timeline -> Option D
  4. Quick Check:

    CPU profiling = CPU Profiler plugin + workload + timeline [OK]
Hint: Use CPU Profiler plugin and run workload to see spikes [OK]
Common Mistakes:
  • Ignoring Flipper and using unrelated npm packages
  • Using Layout plugin for CPU profiling
  • Restarting app without profiling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard