Skip to content

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

Closed
lianrenjie opened this issue Jul 2, 2019 · 15 comments
Closed

Invariant Violation: Minified React error #321 #16029

lianrenjie opened this issue Jul 2, 2019 · 15 comments

Comments

@lianrenjie
Copy link

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?

@cindyrise
Copy link

when import react hook,Invariant Violation: Minified React error #321;The current used react 16.8.6,chrome 75,

@sagar-gavhane
Copy link

sagar-gavhane commented Jul 9, 2019

Hey, Can you provide code snippet or codesandbox link for reference?

@fizikst
Copy link

fizikst commented Jul 14, 2019

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.
Screenshot_11

If the module non contain react-hook (release 1.0.2), return module.
Screenshot_12

All rules https://reactjs.org/warnings/invalid-hook-call-warning.html are executed.
Screenshot_6

@ngocketit
Copy link

I got the same error over here.

@Dergash
Copy link

Dergash commented Aug 5, 2019

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:
another-react

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.
It does not actually handling your js bundle.

@deligent-ant
Copy link

deligent-ant commented Jan 5, 2020

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"
but the #321 still exist,who can help 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'
      }
    }
}

if i don't use useCallback ant useMemo ,it can work,but i need useMemo...

@deligent-ant
Copy link

i solute it . what my question is . npm-link
webpack

alias:{
react: path.resolve('./node_modules/react')
  }

@DamengRandom
Copy link

There might be another possibe case is we try to write hooks code inside class based component, which will cause this error as well.

@codedart2018
Copy link

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"
but the #321 still exist,who can help 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'
      }
    }
}

if i don't use useCallback ant useMemo ,it can work,but i need useMemo...

thank you

@mqtik
Copy link

mqtik commented Jun 13, 2020

I had to comment the following code in order to solve the #321 error, similar to development config.

 externals: [
    // nodeExternals(),
    {
      react: {
        root: 'React',
        commonjs2: 'react',
        commonjs: 'react',
        amd: 'react'
      },
      'react-dom': {
        root: 'ReactDOM',
        commonjs2: 'react-dom',
        commonjs: 'react-dom',
        amd: 'react-dom'
      }
    }
}

Now it works when I build with yarn start, but in release build it crashes at startup.
Weird.

@alexander-elgin
Copy link

alexander-elgin commented Aug 6, 2020

I had the same issue when I used useSelector (from react-redux) in JSX (returned by a render function). The issue was gone when I moved the useSelector to the body of the render function (before the return statement).

@SilvaQ
Copy link

SilvaQ commented Aug 21, 2020

I had to comment the following code in order to solve the #321 error, similar to development config.

 externals: [
    // nodeExternals(),
    {
      react: {
        root: 'React',
        commonjs2: 'react',
        commonjs: 'react',
        amd: 'react'
      },
      'react-dom': {
        root: 'ReactDOM',
        commonjs2: 'react-dom',
        commonjs: 'react-dom',
        amd: 'react-dom'
      }
    }
}

Now it works when I build with yarn start, but in release build it crashes at startup.
Weird.

same question。

@mazunind
Copy link

As @DamengRandom has suggested, rewriting some components into functional ones solved this for me

@gleiderr
Copy link

gleiderr commented Nov 7, 2020

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:

making my app and library point to the same react (package) location.

Below are the steps I followed :

  1. In Your Application:
    a) cd node_modules/react && npm link
    b) cd node_modules/react-dom && npm link

  2. In Your Library
    a) npm link react
    b) npm link react-dom

3)Stop your dev-server and do npm start again.

Refer below for more details:
https://stackoverflow.com/questions/56021112/react-hooks-in-react-library-giving-invalid-hook-call-error

@Statfine
Copy link

you can move react and react-dom from dependencies to devDependencies in your lib,this can ensure react consistency in project

// package.json
{
  "dependencies": {},
  "devDependencies": {
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0"
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests