-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hooks error from external component #15050
Comments
Please provide a reproducing example as a GitHub project we can clone and try. |
Isn't this exactly what the link (https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react) suggests?
This should work (and you say it does). If you're unhappy with this workaround, please investigate this with your bundler (webpack) or your package manager (e.g. Yarn supports Workspaces which solves this by hoisting). I'm not sure I see what is actionable for React in this issue. |
To clarify again, you will only see this issue if you bundle two copies of React in your app — which is bad by itself. It's not a problem with Hooks, but a problem with your bundling process. Bundling duplicate React is bad for your users too because it increases the payload. |
Thanks for answers, There is something incomprehensible for us ... I've created 2 repo to let you see our code : We're also looking about yarn workspaces and webpack to see if there is a solution using them... If you have some advices on how to do with this kind of development, thanks in advance |
Check out these resources: https://reactjs.org/community/support.html We use issue tracker for bugs in React. But this is not a bug, it’s intended behavior. If you have duplicate React in the bundle you’ll have issues. You need to figure out why you have a duplicate React, and fix the root of the problem. Since that depends more on how you bundle the app than on React itself, I think asking elsewhere makes more sense. |
the same error to me. "peerDependencies": {
"antd": "^3.25.0",
"styled-components": "^4.4.1",
"react": "^16.11.0",
"react-dom": "^16.11.0"
} externals: [
// nodeExternals(),
{
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
}
} but still #321 error。 if i don't use useCallback ant useMemo in my npm comoponent,it can work,but i need useMemo... |
If there any one still stucked with this problem,
and
This causes bundler to import two React modules, as Dan pointed. ( #15050 (comment) ) |
same problem using component from a custom library... hooks fail "peerDependencies": { I have done verification of React instace is the same // Add this in node_modules/react-dom/index.js // Add this in your component file |
Hi guys, i have fixed this issue please try this out. Component composition issue react i really liked its has detailed info about external component related issue |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
We are trying to create an external module which will be imported in main app.
Here is the module :
Importing this simple code into main app gives us the error :
Removing hooks this code works fine.
We have followed instructions from https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react but error still thrown
If we link react as mentionned in documentation, works but it only can be done in development not in production.
We are not the only one having this issue Side package with React Hooks failing with Invariant Violation when called in main package
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
Code from external component :
package.json
webpack.config.js
src/index.js
src/components/hello.js
Code from main app:
webpack.config.js
package.json
src/index.js
To be able to make it works, you have to do
yarn link
after (or before) having built it from component andyarn link "app-component"
from main app.What is the expected behavior?
use hooks from external components works obviously.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.8.4 and with previous version it doest not work too
The text was updated successfully, but these errors were encountered: