gpt4 book ai didi

reactjs - 如何在我的组件库中使用 React hooks?

转载 作者:行者123 更新时间:2023-12-03 13:31:08 27 4
gpt4 key购买 nike

我目前正在创建一个组件库,该库通过 NPM 链接包含在 Nextjs 项目中,但很难让它一致加载而不会出现错误。最新的是

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

这是一个 Typescript 组件,但据我所知,我所做的还不错,但它只是不玩运动。代码如下:

        import React, { useEffect } from "react";
import { FlagIcon } from "react-flag-kit";

const LanguageSwitcher = () => {
useEffect(() => {
alert('Yo!');
})
return (
<div className="w-full justify-end">
<button>
<div className="flex items-center">
<FlagIcon code="GB" /> <span className="block ml-2">English</span>
</div>
</button>
</div>
)
}

export default LanguageSwitcher

然后将其导入到库中的另一个组件中,然后导入到 Nextjs 页面中。这就是错误出现的地方。

我不确定这是否是 webpack 或 typescript 编译问题,尝试了我能想到的所有不同目标,尝试谷歌搜索各种内容,但似乎没有任何内容与我的情况相关。它甚至可能不是 React 问题,而是 Typescript 和 Webpack 的问题,但我对这些问题的接触是非常新的。

我的 webpack 配置:

        var path = require('path');
module.exports = {
entry: './src/index.ts',
devtool: "source-map",
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js',
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.ts(x?)$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
}
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
}
};

我的tsconfig.json


{
"compilerOptions": {
"outDir": "build",
"module": "esnext",
"target": "es6",
"lib": ["es6", "dom", "es2016", "es2017"],
"sourceMap": true,
"allowJs": false,
"jsx": "react",
"declaration": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true
},
"include": ["src"],
"exclude": ["node_modules", "build"]
}

任何指示或帮助将不胜感激!

我也尝试将所有内容恢复为 vanilla JS,但问题仍然存在,所以我认为这可能与 webpack 有关?

编辑

这是我的完整 package.json 文件以及 Nextjs 的内容

{
"name": "some/package",
"version": "1.0.0",
"description": "",
"main": "build/index.js",
"scripts": {
"test": "jest",
"start": "webpack --watch --mode=development",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "16.10.2",
"webpack": "^4.41.1"
},
"devDependencies": {
// ...
"@types/react": "^16.9.5",
"@types/react-dom": "^16.9.1",
"react": "16.10.2",
"react-dom": "^16.10.2",
"ts-loader": "^6.2.0",
"typescript": "^3.6.4",
"webpack-cli": "^3.3.9"
}
}
    "react": "16.10.2",
"react-dom": "16.10.2"

最佳答案

事实证明,这是由于组件库中的符号链接(symbolic link) React 出现问题

https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react

通过运行链接中的步骤修复:

npm link ../myapp/node_modules/react // Run within the component library

关于reactjs - 如何在我的组件库中使用 React hooks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58383048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com