tsdx - 反应为空,防止挂钩使用

tsdx - react is null, prevents hook usage

我最近尝试使用 tsdx 创建一个 react typescript 组件进行编译,但在调试时遇到了一些困难。该包已发布,并且似乎正在发布和安装,没有错误。然而,当谈到实际使用时,我总是会遇到:

TypeError: Cannot read properties of null (reading 'useEffect').

我遇到了与 useContext 相同的问题,所以简单地替换了功能,但现在错误已迁移到 useEffect

这个包在本地运行完全正常,如果我删除所有代码并手动将其注入到应用程序中,它可以毫无问题地运行。最初我只是解构 react 中的 useEffect 钩子,但即使显式导入 reactimport React from 'react'; react 显然仍然是 null...

查看 .cjs 文件,我可以看到正在导入 React:

var React = _interopDefault(require('react'));

但问题仍然存在。

感谢任何和所有的帮助,如果有帮助,会粘贴我的 package.json 中的一些信息:)

{
  "name": "xxxx",
  "version": "x.x.x",
  "description": "xxxx",
  "author": "xxx xxx",
  "keywords": [],
  "main": "dist/index.js",
  "module": "dist/xxx.esm.js",
  "typings": "dist/index.d.ts",
  "engines": {
    "node": ">=10"
  },
  "files": [
    "dist",
    "src",
    "README.md"
  ],
  "repository": {
    "type": "git",
    "url": "git+https://github.com/xxxx/xxxx.git"
  },
  "dependencies": {
    "@babel/polyfill": "^7.12.1",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.2.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.0",
    "@types/node": "^16.11.34",
    "@types/react": "^18.0.9",
    "@types/react-dom": "^18.0.3",
    "axios": "^0.27.2",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.6.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "npx tsdx build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.17.10",
    "@babel/core": "^7.17.12",
    "@babel/preset-env": "^7.17.12"
  },
  "bugs": {
    "url": "https://github.com/xxxx/xxxx/issues"
  },
  "homepage": "https://github.com/xxxx/xxxx#readme",
  "license": "ISC"
}

编辑: 此外,如果值得注意 - 该项目未初始化为 tsdx 项目,我将 tsdx 构建脚本和一些元素添加到 tsconfig 和 package.json after 运行 into packaging issues prior to switching

只是给遇到这个问题的其他人留下便条,在我的包中,我在 package.json 的常规 dependencies 对象中有“反应”和许多其他依赖项,问题似乎当我将这些移动到 devDependencies.

时解决