Fast Refresh is a React Native feature that allows you to get near-instant feedback for changes in your React components. Fast Refresh is enabled by default, and you can toggle "Enable Fast Refresh" in the React Native developer menu. With Fast Refresh enabled, most edits should be visible within a second or two.
Button.js
and Modal.js
import Theme.js
, editing Theme.js
will update both components.If you make a syntax error during a Fast Refresh session, you can fix it and save the file again. The redbox will disappear. Modules with syntax errors are prevented from running, so you won't need to reload the app.
If you make a runtime error during the module initialization (for example, typing Style.create
instead of StyleSheet.create
), the Fast Refresh session will continue once you fix the error. The redbox will disappear, and the module will be updated.
If you make a mistake that leads to a runtime error inside your component, the Fast Refresh session will also continue after you fix the error. In that case, React will remount your application using the updated code.
If you have error boundaries in your app (which is a good idea for graceful failures in production), they will retry rendering on the next edit after a redbox. In that sense, having an error boundary can prevent you from always getting kicked out to the root app screen. However, keep in mind that error boundaries shouldn't be too granular. They are used by React in production, and should always be designed intentionally.
Fast Refresh tries to preserve local React state in the component you're editing, but only if it's safe to do so. Here's a few reasons why you might see local state being reset on every edit to a file:
createNavigationContainer(MyScreen)
. If the returned component is a class, state will be reset.In the longer term, as more of your codebase moves to function components and Hooks, you can expect state to be preserved in more cases.
// @refresh reset
anywhere in the file you're editing. This directive is local to the file, and instructs Fast Refresh to remount components defined in that file on every edit.When possible, Fast Refresh attempts to preserve the state of your component between edits. In particular, useState
and useRef
preserve their previous values as long as you don't change their arguments or the order of the Hook calls.
Hooks with dependencies—such as useEffect
, useMemo
, and useCallback
—will always update during Fast Refresh. Their list of dependencies will be ignored while Fast Refresh is happening.
For example, when you edit useMemo(() => x * 2, [x])
to useMemo(() => x * 10, [x])
, it will re-run even though x
(the dependency) has not changed. If React didn't do that, your edit wouldn't reflect on the screen!
Sometimes, this can lead to unexpected results. For example, even a useEffect
with an empty array of dependencies would still re-run once during Fast Refresh. However, writing code resilient to an occasional re-running of useEffect
is a good practice even without Fast Refresh. This makes it easier for you to later introduce new dependencies to it.
© 2022 Facebook Inc.
Licensed under the Creative Commons Attribution 4.0 International Public License.
https://reactnative.dev/docs/fast-refresh