Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 创建不使用样式化组件的react库_Reactjs - Fatal编程技术网

Reactjs 创建不使用样式化组件的react库

Reactjs 创建不使用样式化组件的react库,reactjs,Reactjs,我正在使用createreact-library构建我的组件库,并且我尝试使用样式化的组件,但是我一直得到一个react-hooks错误 这是我的密码: 下面是错误: Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have m

我正在使用
createreact-library
构建我的组件库,并且我尝试使用样式化的组件,但是我一直得到一个react-hooks错误

这是我的密码:

下面是错误:

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

我没有在任何地方使用React钩子,我的React和React DOM版本匹配,我没有React的多个版本。

检查您的package.json文件您似乎有React/React DOM版本16的依赖项,但有版本15的对等依赖项。我认为这是问题的原因,请删除对等依赖项。此外,如果您所做的只是使用功能组件方法进行渲染,则无需将组件编写为基于类的组件。

附带的示例项目中的依赖项非常过时

相反,我设置了一个新的单独示例项目,它完美地呈现了您的按钮:

npx create-react-app component-library-example
cd component-library-example
npm install --save emmas-super-cool-component-library
npm start
App.js:

从“React”导入React;
从“emmas super cool组件库”导入{PrimaryButton};
常量应用=()=>(
{
警报(“点击”);
}}
>
文本
);
导出默认应用程序;
结果:


我最近使用了createreact库,遇到了同样的问题。在这种情况下,错误消息完全有意义。当您实际发布包并在使用者应用程序中使用它时,不会发生此问题。这意味着,问题在于您的
示例
文件夹。如果您查看
package.json
,它位于
example
文件夹中,您可以看到react、react脚本、react dom等的库引用。使用下面的文件协议引用它们可以解决您的问题

  "dependencies": {
    "library-name": "file:..",
    "react": "file:../node_modules/react",
    "react-dom": "file:../node_modules/react-dom",
    "react-scripts": "file:../node_modules/react-scripts"
    "node-sass": "file:../node_modules/node-sass"
  },

在尝试此操作之前,请清除您的
节点\u模块
以及
包锁.json

我删除了对等依赖项,但没有解决问题。我知道我可以把它们写成SFC,但这是库中的样板文件,我没有重构它。我会克隆你的库并运行它。然后回复你。你似乎刚刚更新了你的对等依赖版本,为什么需要对等依赖/你不能删除这些吗?从其他类似的问题上,他们建议删除你的package.json锁文件和节点模块,然后重新运行安装。请告诉我如何运行你的应用程序。我认为发布的版本和有问题的版本不同。没有从她面临错误的版本中导出PrimaryButton我没有将当前版本发布到npm,因为它太坏了。发布的版本是一个普通的React按钮,因此它肯定会呈现:/