npm Minified React错误#321;访问https://reactjs.org/docs/error-decoder.html?invariant=321

h5qlskok  于 11个月前  发布在  React
关注(0)|答案(5)|浏览(193)

我正在尝试创建几个react组件,并将其作为包发布到npm中,以便其他项目可以通过导入包来使用它们。
我的软件包是“样品测试”,可在(https://www.npmjs.com/package/sample-testing)。
使用“webpack”和“babel loader”来translate jsx文件。
当我试图使用这个包中的组件时,出现以下错误。

**Minified React error #321;访问https://reactjs.org/docs/error-decoder.html?invariant=321获取完整消息,或使用非minified dev环境获取完整错误和其他有用警告。

这个错误是来只有当我使用材料的ui组件内包,而不是与常规的html组件。

**.babelrc

{
 "presets": ["react", "env", "stage-0"]
  }

字符串

wepack.config.js

var path = require("path");

module.exports = {
  mode: "production",
  entry: {
    TButton: "./src/TButton.jsx"
  },
  output: {
    path: path.resolve("lib"),
    filename: "[name].js",
    libraryTarget: "commonjs2"
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules)/,
        use: "babel-loader"
      }
    ]
  }
};

TButton.jsx

import React from "react";

import TextField from "@material-ui/core/TextField";

class TButton extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <TextField id="standard-basic" label="Standard" />;
  }
}

export default TButton;


这里是codesandbox链接(https://codesandbox.io/s/xenodochial-mcclintock-xtkh6?file=/src/index.js)复制的错误,我在我的项目中使用时,我在我的项目。请帮助我解决它。

k10s72fa

k10s72fa1#

当运行时中应用了不兼容的react版本时,也出现了同样的错误。必须在主package.json文件中更改react和react-dom版本。

"dependencies": {
  "react": "^17.0.2",
  "react-dom": "^17.0.2",
  ...
 }

字符串

yrdbyhpb

yrdbyhpb2#

我也有同样的错误,但是在使用rollup时。我通过将reactreact-dom移动到package.json中的peerDependencies来解决它

"peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },

字符串

sbtkgmzw

sbtkgmzw3#

此错误原因是从“react/cjs/react.production.min”导入{ useState };请从“react”导入

yfwxisqw

yfwxisqw4#

DSA
“peeradministrations”:{“react”:“^17.0.2”,“react-dom”:“^17.0.2”},

nzk0hqpo

nzk0hqpo5#

一个原因可能是您从错误的位置导入useEffect(可能是IDE做的)

import {useEffect} from "react/cjs/react.production.min";

字符串

import React, {useEffect} from 'react';

相关问题