You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Don’t call Hooks inside loops, conditions, or nested functions.** Instead, always use Hooks at the top level of your React function, before any early returns. You can only call Hooks while React is rendering a function component:
[Custom Hooks](/learn/reusing-logic-with-custom-hooks)*may* call other Hooks (that's their whole purpose). This works because custom Hooks are also supposed to only be called while a function component is rendering.
## Mismatching Versions of React and React DOM {/*mismatching-versions-of-react-and-react-dom*/}
114
+
## 版本不一致的 React 和 React DOM {/*mismatching-versions-of-react-and-react-dom*/}
115
115
116
-
You might be using a version of `react-dom` (< 16.8.0) or `react-native` (< 0.59) that doesn't yet support Hooks. You can run `npm ls react-dom`or`npm ls react-native`in your application folder to check which version you're using. If you find more than one of them, this might also create problems (more on that below).
116
+
你可能正在使用一个还不支持 Hook 的版本,例如 `react-dom`(< 16.8.0)或 `react-native`(< 0.59)。你可以在你的应用目录下执行 `npm ls react-dom`或`npm ls react-native`来检查下你正在使用的版本。如果你发现了同一个包有多个版本,那也可能带来其他的问题(下文会详细展开)。
117
117
118
-
## Duplicate React {/*duplicate-react*/}
118
+
## 重复的 React {/*duplicate-react*/}
119
119
120
-
In order for Hooks to work, the `react`import from your application code needs to resolve to the same module as the `react` import from inside the `react-dom` package.
If these `react`imports resolve to two different exports objects, you will see this warning. This may happen if you **accidentally end up with two copies**of the `react`package.
If you use Node for package management, you can run this check in your project folder:
124
+
如果你用的是 Node 进行包管理,你可以在你的应用目录下执行这个命令做个检查:
125
125
126
126
<TerminalBlock>
127
127
128
128
npm ls react
129
129
130
130
</TerminalBlock>
131
131
132
-
If you see more than one React, you'll need to figure out why this happens and fix your dependency tree. For example, maybe a library you're using incorrectly specifies `react`as a dependency (rather than a peer dependency). Until that library is fixed, [Yarn resolutions](https://yarnpkg.com/lang/en/docs/selective-version-resolutions/)is one possible workaround.
You can also try to debug this problem by adding some logs and restarting your development server:
134
+
你也可以通过增加一些日志,然后重启你的开发服务器,这样你就可以自己来调试这个问题了:
135
135
136
136
```js
137
-
//Add this in node_modules/react-dom/index.js
137
+
//把下面这行加在 node_modules/react-dom/index.js
138
138
window.React1=require('react');
139
139
140
-
//Add this in your component file
140
+
//把下面这几行加入到你的组件逻辑中
141
141
require('react-dom');
142
142
window.React2=require('react');
143
143
console.log(window.React1===window.React2);
144
144
```
145
145
146
-
If it prints `false` then you might have two Reacts and need to figure out why that happened. [This issue](https://github.com/facebook/react/issues/13991)includes some common reasons encountered by the community.
This problem can also come up when you use `npm link`or an equivalent. In that case, your bundler might "see" two Reacts — one in application folder and one in your library folder. Assuming `myapp`and`mylib`are sibling folders, one possible fix is to run `npm link ../myapp/node_modules/react` from `mylib`. This should make the library use the application's React copy.
In general, React supports using multiple independent copies on one page (for example, if an app and a third-party widget both use it). It only breaks if `require('react')` resolves differently between the component and the `react-dom`copy it was rendered with.
If none of this worked, please comment in [this issue](https://github.com/facebook/react/issues/13991)and we'll try to help. Try to create a small reproducing example — you might discover the problem as you're doing it.
0 commit comments