Each time you save a JavaScript file in a React Native app, Fast Refresh decides how much of the running app to update: one component, a group of modules or the whole app. Whether the form you half filled in survives the save depends on that choice. Knowing it also tells you when to reload on purpose, since JavaScript edits reach the app without a rebuild of the native app.

Key terms

  • Fast Refresh: the development feature, on by default, that applies a saved edit to the running app, with most edits visible within a second or two.
  • Full reload: a restart of the app's JavaScript that resets all of its state.
  • Dev Menu: the in-app developer menu of a development build, with the Reload item and the Fast Refresh toggle.
  • // @refresh reset: a comment that makes Fast Refresh remount the components defined in its file on every edit.

Three scopes for one save

What the edited module exports, and who imports it, sets the scope of the update.

A module that only exports React components gets the narrowest one. Fast Refresh updates the code of that module alone and re-renders the component. You can change styles, rendering logic, event handlers and effects in that file and the update stays this narrow.

When a module's exports aren't React components, Fast Refresh re-runs that module and every module that imports it. Edit a spacing.ts file that both Card.tsx and Header.tsx import, and both components update.

If modules outside the React tree import the file, Fast Refresh falls back to a full reload. Say a component file also exports a constant and a non-React utility module imports that constant. From then on every save of the component file reloads the app. Move the constant into a file of its own, import it from both places, and edits to the component file go back to Fast Refresh.

Which state survives the update

Fast Refresh keeps the local React state of the component you edit, but only when that is safe. Function components and Hooks keep their state by default, while class components never do. State is also reset when:

  • the edited module exports something in addition to a React component
  • the module exports the result of a higher-order component call, such as withAuth(ProfileScreen), and the component it returns is a class

Within a function component, useState and useRef keep their previous values as long as you change neither their arguments nor the order of the Hook calls. Change useState(1) to useState(5) and that guarantee is gone.

Hooks with dependencies behave the other way. useEffect, useMemo and useCallback always update during Fast Refresh, and their dependency lists are ignored while it happens. That is why an edit to a memoized calculation shows up on screen even when none of its inputs changed. An empty list is no exception. A useEffect with [] runs once more during Fast Refresh, so an effect that subscribes or logs should cope with running again, which is good practice even without Fast Refresh.

Reading a save in the Basket component

The Basket component is the only export of its file, so a save stays in the narrowest scope. Suppose you have raised the quantity to 3 and then change the button title. After the save,

tsx
const [quantity, setQuantity] = useState(1);

still holds 3, because neither the argument nor the Hook order changed. The useMemo that computes total runs again although items and quantity are the same, and the effect with [] logs Basket mounted a second time.

Forcing a remount

Sometimes you want the reset, for example while you tweak an animation that only runs on mount. Add // @refresh reset anywhere in the file. The directive applies to that file only, and Fast Refresh then remounts the components defined in it on every edit.

Reloading and the Dev Menu

For a clean start, do a full reload. In an Android emulator, press R twice in quick succession (within 200 milliseconds, and not while a text field has focus) or choose Reload in the Dev Menu. In the iOS Simulator, press R, or Cmd + R.

Open the Dev Menu by shaking the device, or with these shortcuts:

  • iOS Simulator: Ctrl + Cmd + Z, or Device > Shake
  • Android emulator: Cmd + M on macOS, Ctrl + M on Windows and Linux, or adb shell input keyevent 82

The same menu holds the Fast Refresh toggle, labeled "Disable Fast Refresh" while it is on and "Enable Fast Refresh" while it is off. Release builds disable the Dev Menu altogether. If the app loses its connection to Metro, saves stop arriving and the app shows Fast Refresh disconnected. Reload app to reconnect.

Code Examples

tsx
import {useEffect, useMemo, useState} from 'react';
import {Button, Text, View} from 'react-native';

type BasketItem = {name: string; price: number};

export default function Basket({items}: {items: BasketItem[]}) {
  // Keeps its value across a save while useState(1) and the Hook order stay the same
  const [quantity, setQuantity] = useState(1);

  // Recomputed on every Fast Refresh: the dependency list is ignored while it runs
  const total = useMemo(
    () => items.reduce((sum, item) => sum + item.price, 0) * quantity,
    [items, quantity],
  );

  useEffect(() => {
    // Runs on mount, and once more during each Fast Refresh despite the empty list
    console.log('Basket mounted');
  }, []);

  return (
    <View>
      <Text>Quantity: {quantity}</Text>
      <Text>Total: {total.toFixed(2)}</Text>
      <Button title="Add one" onPress={() => setQuantity(quantity + 1)} />
    </View>
  );
}
✓ Completed