如何在组件库中使用React挂钩?

时间:2019-10-14 19:34:25

标签: reactjs webpack react-hooks next.js

我目前正在创建一个组件库,该组件库通过NPM链接包含在Nextjs项目中,并且很难使其始终如一地加载而不会出错。最新的是

  

无效的挂钩调用。挂钩只能在功能组件的主体内部调用。发生这种情况可能是由于以下原因之一

这是一个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还是打字稿编译问题,尝试过我能想到的所有不同目标,尝试过搜索各种事物,但似乎与我的情况无关。甚至可能不是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"]
    }

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

我还尝试将所有内容恢复为原始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"

2 个答案:

答案 0 :(得分:0)

原来这是由于组件库中的符号链接反应问题

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

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

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

答案 1 :(得分:0)

面临同样的问题。但是,组件库不归我所有,因此不得不使用此 dependency resolution by 'Yarn'

这是我最初的依赖树

project
|- componentLibrary
|  |- react@16.10
|  |- react-dom@16.10
|- react@17.0.1
|- react-dom@17.0.1

在我的 package.json 中添加了以下代码片段

"resolutions": {
  "componentLibrary/react": "17.0.1",
  "componentLibrary/react-dom": "17.0.1"
},

解决了我对项目 reactreact-dom 的依赖。