-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Invariant Violation: Minified React error #321 #16029
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
Comments
when import react hook,Invariant Violation: Minified React error #321;The current used react 16.8.6,chrome 75, |
Hey, Can you provide code snippet or codesandbox link for reference? |
I make a module module-hook (https://codesandbox.io/s/little-dawn-2sxyo). To check the functionality of the module using https://github.com/facebook/create-react-app. If the module contain react-hook (release 1.0.0), return same error. If the module non contain react-hook (release 1.0.2), return module. All rules https://reactjs.org/warnings/invalid-hook-call-warning.html are executed. |
I got the same error over here. |
Hello @lianrenjie, @fizikst, @cindyrise, @ngocketit! Based on the example that @fizikst provided I believe the problem is that you are indeed have multiple instances of React on your page and the warning invariant=321 is correct about that. You can see it in your bundled code in your npm package: The thing is, aside of making React and react-dom peer deependencies in your libraries package.json, you also should mark them as 'externals' when building them with webpack, so webpack would know that react shouldn't be bundled into your library and should be looked in runtime instead. I made a PR for a @fizikst example which should remove another react copy from bundled js file - I probably mangled it a bit with different library types but error should go away and you may find further reading about it here peerDependencies section is more like a hint for your published package, saying how to solve your package dependencies when installing it as a library for other app, which version to install and which version should be provided by your other app. |
i have did as your said, just like . " aside of making React and react-dom peer deependencies in your libraries package.json, you also should mark them as 'externals' when building them with webpack" "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'
}
}
} if i don't use useCallback ant useMemo ,it can work,but i need useMemo... |
i solute it . what my question is . npm-link alias:{
react: path.resolve('./node_modules/react')
} |
There might be another possibe case is we try to write hooks code inside class based component, which will cause this error as well. |
thank you |
I had to comment the following code in order to solve the #321 error, similar to development config.
Now it works when I build with |
I had the same issue when I used |
same question。 |
As @DamengRandom has suggested, rewriting some components into functional ones solved this for me |
I am using my package as my-lib: "link: ../ .." while testing. That is why this problem occurred. What worked for me was the suggestion found in the stackoverflow below:
Refer below for more details: |
you can move react and react-dom from dependencies to devDependencies in your lib,this can ensure react consistency in project
|
Do you want to request a feature or report a bug?
What is the current behavior?
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:
What is the expected behavior?
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
The text was updated successfully, but these errors were encountered: