【问题标题】:Creating react/typescript library resulting in Invalid hook call error创建 react/typescript 库导致 Invalid hook call 错误
【发布时间】:2023-02-08 22:34:54
【问题描述】:

我正在构建一个 react/typescript 库,使用 typescript 和 rollup 来导出它 这是我的图书馆package.json

     {
      "name": "infosysta-typescript-core",
      "version": "1.0.0",
      "description": "This will the infosysta's core library for typescript",
      "scripts": {
        "build_tsc": "tsc",
        "build_publish": "tsc -p .",
        "publish": "npm publish",
        "start": "tsc && nodemon build/index.js",
        "rollup": "rollup -c"
      },
      "keywords": [],
      "author": "Infosysta",
      "license": "ISC",
      "devDependencies": {
        "@atlaskit/adf-schema": "^25.1.1",
        "@atlaskit/button": "^13.4.2",
        "@atlaskit/editor-json-transformer": "^8.7.6",
        "@atlaskit/editor-wikimarkup-transformer": "^5.7.1",
        "@atlaskit/form": "^7.4.1",
        "@atlaskit/icon": "^20.1.2",
        "@atlaskit/mention": "^21.0.9",
        "@atlaskit/section-message": "^6.3.9",
        "@atlaskit/spinner": "^14.0.0",
        "@atlaskit/textfield": "^3.1.13",
        "@atlaskit/tooltip": "^9.0.0",
        "@babel/core": "^7.16.0",
        "@babel/preset-env": "^7.16.4",
        "@babel/preset-react": "^7.16.0",
        "@babel/preset-typescript": "^7.16.0",
        "@fluentui/react": "^7.137.1",
        "@mdx-js/react": "^2.1.2",
        "@rollup/plugin-commonjs": "^21.0.1",
        "@rollup/plugin-image": "^3.0.2",
        "@rollup/plugin-json": "^6.0.0",
        "@rollup/plugin-node-resolve": "^13.0.6",
        "@rollup/plugin-typescript": "^8.3.0",
        "@tanker/file-ponyfill": "^2.22.0",
        "@types/lodash-es": "^4.17.6",
       
        
        "@types/validator": "^13.7.11",
        "adf-builder": "^3.3.0",
        "axios": "^0.21.1",
        "deepmerge": "^4.3.0",
        "lodash-es": "^4.17.21",
        "moment": "^2.29.1",
        "nodemon": "^2.0.19",
        "prosemirror-model": "^1.19.0",   
       
        "rollup": "^2.60.0",
        "rollup-plugin-copy-assets": "^2.0.3",
        "rollup-plugin-dts": "^4.0.1",
        "rollup-plugin-postcss": "^4.0.1",
        "socket.io": "^2.4.1",
        "socket.io-client": "^2.3.0",
        "typescript": "^4.7.4",
        "validator": "^13.6.0",
        "react-router-dom": "^5.2.0",
        "@types/react-router-dom": "^5.3.3"
      },
      "peerDependencies": {
    "react": "^16.14.0",
    "react-dom": "^16.10.2",
    "@types/react": "^16.8.22",
    "@types/react-dom": "^16.8.4"
  },
      "main": "dist/cjs/index.js",
      "module": "dist/esm/index.js",
      "files": [
        "dist"
      ],
      "types": "dist/index.d.ts"
    }

以下是创建错误的项目的package.json(请注意,除了 peerDependecies 中的那些之外,package.json 中没有其他 react 参考)

 "peerDependencies": {
"@types/react": "^16.8.22",
"@types/react-dom": "^16.8.4",
"react": "^16.14.0",
"react-dom": "^16.10.2"
 },

错误如下

Uncaught 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:

【问题讨论】:

  • package.json在这里用处不大,真正有用的是code sn-ps。
  • 几乎可以确定在某个地方有一个无效的钩子调用你的代码。你需要向我们展示这一点。这是可能的它可能是由页面上有多个版本的 React 引起的,但那是极其与仅进行无效调用相比不太可能。

标签: javascript reactjs typescript


【解决方案1】:

您应该确保按照此地址link 的规则进行操作

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2021-03-20
  • 2021-05-27
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
相关资源
最近更新 更多