快速刷新
快速刷新是一项 React Native 功能,可让你获得有关 React 组件更改的近乎即时的反馈。快速刷新默认启用,你可以在 React Native 开发菜单 中切换 "启用快速刷新"。启用快速刷新后,大多数编辑应在一两秒内可见。
¥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 Dev Menu. With Fast Refresh enabled, most edits should be visible within a second or two.
怎么运行的
¥How It Works
-
如果你编辑仅导出 React 组件的模块,快速刷新将仅更新该模块的代码,并重新渲染你的组件。你可以编辑该文件中的任何内容,包括样式、渲染逻辑、事件处理程序或效果。
¥If you edit a module that only exports React component(s), Fast Refresh will update the code only for that module, and re-render your component. You can edit anything in that file, including styles, rendering logic, event handlers, or effects.
-
如果你编辑导出的模块不是 React 组件,则快速刷新将重新运行该模块以及导入该模块的其他模块。因此,如果
Button.js
和Modal.js
都导入Theme.js
,则编辑Theme.js
将更新这两个组件。¥If you edit a module with exports that aren't React components, Fast Refresh will re-run both that module, and the other modules importing it. So if both
Button.js
andModal.js
importTheme.js
, editingTheme.js
will update both components. -
最后,如果你编辑由 React 树外部的模块导入的文件,快速刷新将回退到执行完全重新加载。你可能有一个文件,它渲染 React 组件,但也导出由非 React 组件导入的值。例如,也许你的组件还导出一个常量,并且非 React 实用程序模块导入它。在这种情况下,请考虑将常量迁移到单独的文件并将其导入到两个文件中。这将重新启用快速刷新功能。其他情况通常也可以用类似的方法解决。
¥Finally, if you edit a file that's imported by modules outside of the React tree, Fast Refresh will fall back to doing a full reload. You might have a file which renders a React component but also exports a value that is imported by a non-React component. For example, maybe your component also exports a constant, and a non-React utility module imports it. In that case, consider migrating the constant to a separate file and importing it into both files. This will re-enable Fast Refresh to work. Other cases can usually be solved in a similar way.
错误恢复能力
¥Error Resilience
如果在快速刷新会话期间出现语法错误,你可以修复它并再次保存文件。红框将会消失。存在语法错误的模块将被阻止运行,因此你无需重新加载应用。
¥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.
如果你在模块初始化期间发生运行时错误(例如,键入 Style.create
而不是 StyleSheet.create
),则修复错误后快速刷新会话将继续。红框将消失,模块将更新。
¥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.
如果你犯了一个错误,导致组件内部出现运行时错误,则快速刷新会话在你修复错误后也将继续。在这种情况下,React 将使用更新的代码重新安装你的应用。
¥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.
如果你的应用中有 误差边界(这对于生产中的优雅失败来说是一个好主意),它们将在红框之后的下一次编辑中重试渲染。从这个意义上说,拥有错误边界可以防止你总是被踢到根应用屏幕。但是,请记住,错误边界不应该太细粒度。它们由 React 在生产中使用,并且应始终有意设计。
¥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.
局限性
¥Limitations
快速刷新尝试在你正在编辑的组件中保留本地 React 状态,但前提是这样做是安全的。以下是你可能会在每次编辑文件时看到本地状态被重置的几个原因:
¥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:
-
类组件不保留本地状态(只有函数组件和 Hook 保留状态)。
¥Local state is not preserved for class components (only function components and Hooks preserve state).
-
除了 React 组件之外,你正在编辑的模块可能还有其他导出。
¥The module you're editing might have other exports in addition to a React component.
-
有时,模块会导出调用高阶组件(如
createNavigationContainer(MyScreen)
)的结果。如果返回的组件是一个类,状态将被重置。¥Sometimes, a module would export the result of calling higher-order component like
createNavigationContainer(MyScreen)
. If the returned component is a class, state will be reset.
从长远来看,随着更多的代码库转移到函数组件和 Hook,你可以期望在更多情况下保留状态。
¥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.
提示
¥Tips
-
默认情况下,快速刷新会保留函数组件(和 Hook)中的 React 本地状态。
¥Fast Refresh preserves React local state in function components (and Hooks) by default.
-
有时你可能想要强制重置状态并重新安装组件。例如,如果你要调整仅在安装时发生的动画,这会很方便。为此,你可以在正在编辑的文件中的任意位置添加
// @refresh reset
。该指令是文件的本地指令,并指示快速刷新在每次编辑时重新安装该文件中定义的组件。¥Sometimes you might want to force the state to be reset, and a component to be remounted. For example, this can be handy if you're tweaking an animation that only happens on mount. To do this, you can add
// @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.
快速刷新和钩子
¥Fast Refresh and Hooks
如果可能,快速刷新会尝试在编辑之间保留组件的状态。特别是,只要你不更改 useState
和 useRef
的参数或 Hook 调用的顺序,它们就会保留其先前的值。
¥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.
具有依赖的钩子(例如 useEffect
、useMemo
和 useCallback
)将始终在快速刷新期间更新。在快速刷新发生时,它们的依赖列表将被忽略。
¥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.
例如,当你将 useMemo(() => x * 2, [x])
编辑为 useMemo(() => x * 10, [x])
时,即使 x
(依赖)没有更改,它也会重新运行。如果 React 不这样做,你的编辑将不会反映在屏幕上!
¥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!
有时,这可能会导致意想不到的结果。例如,即使具有空依赖数组的 useEffect
仍会在快速刷新期间重新运行一次。然而,即使没有快速刷新,编写能够适应偶尔重新运行 useEffect
的代码也是一个很好的做法。这使你以后可以更轻松地向其引入新的依赖。
¥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.