我最近尝试使用tsdx来编译创建一个react typescript组件,并且在调试时遇到了一些困难。包已发布,并且似乎正在发布和安装,没有错误。然而,当实际使用时,我总是遇到:
TypeError: Cannot read properties of null (reading 'useEffect').
我在useContext
中遇到了同样的问题,所以简单地替换了功能,但现在错误已经迁移到useEffect
。
这个包在本地运行得很好,如果我把所有的代码都删掉,然后手动注入到一个应用程序中,它就会正常运行。最初,我只是从react
解构useEffect
钩子,但即使显式导入react
作为import React from 'react';
,react
显然仍然是null
…
查看.cjs
文件,我可以看到React正在导入:
var React = _interopDefault(require('react'));
但问题仍然存在。
任何和所有的帮助是感激的,将粘贴一些信息从我的包。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和包中。在切换
之前遇到打包问题后的Json给遇到这种情况的人留个提示,在我的包里我有"react"和一些其他的依赖关系在一个常规的dependencies
对象在我的package.json
,这个问题似乎解决了,当我移动到devDependencies
。