在开发环境中时,我的应用程序可以正常工作。在生产环境中时,它崩溃并显示以下错误:
Uncaught TypeError: (0 , _react.useEffect) is not a function
这发生在我导入React和useEffect的文件中,如下所示:
import React, { useEffect } from 'react'
const X = () => {
useEffect(() => { ... })
...
}
在此行下添加console.log确认在生产中useEffect确实是未定义的,在dev中则是预期的功能。
我检查了package.json,yarn.lock和node_modules中是否存在引入了useEffect的16.8.0以下的任何react或react-dom版本。但是所有内容都是16.13.1,它们是主要依赖项,我确实尝试清理了纱线缓存,删除了node_modules&yarn.lock,然后重新安装。
我尝试将其添加和删除peerDependencies
失败。
我检查一下以确保没有运行两个独立版本的React,但要保存window.React1 = React
在库和window.React2 = React
我的应用程序中并检查
window.React1 === window.React2
是真的,所以也不是。
最后,我还尝试将React别名为node_modules中的特定别名,但是没有任何运气。
我发现可行的唯一解决方案是按如下方式导入它:
import React from 'react';
const X = () => {
React.useEffect(() => { ... })
...
}
但这应该与使用非结构化导入完全相同吗?如果我确实明确使用React.useEffect,它也会迫使我将所有其他useState和useEffect钩子更改为React.useSate
和React.useEffect
下一个错误就变成:TypeError: (0 , _react.useState) is not a function
在另一个我使用React钩子的文件中。
我想解决不实施解决方法的问题。
我曾经使用microbundle
React将我的图书馆捆绑在一起。我parcel-bundler
用来导入React组件并将其呈现在开发环境(直接从src)或prod(捆绑的库)中
我使用的捆绑版本与.mjs捆绑在一起
我也检查了缩小的.mjs包的输出,并在React中像这样导入:
import ue,{useEffect as pe,useState as fe}from"react";
在我看来,这很好。
我真正不明白的是重组的导入将如何破坏它,但是仅仅执行React.useEffect就可以了吗?
这是我的package.json
{
"name": "xxx",
"version": "1.1.4",
"repository": "git@github.com:xxx/xxx.git",
"author": "xxx",
"license": "MIT",
"source": "src/index.ts",
"main": "dist/bundle.js",
"umd:main": "dist/bundle.umd.js",
"module": "dist/bundle.mjs",
"publishConfig": {
"registry": "https://npm.pkg.github.com/@xxx"
},
"scripts": {
"build": "microbundle",
"dev": "parcel ./test-app/dev/index.html --port 3000",
"start": "parcel ./test-app/serve/index.html --port 3000",
"storybook": "start-storybook -s ./public -c .storybook --ci",
"prepublishOnly": "yarn build"
},
"dependencies": {
"@api-platform/admin": "2.1.0",
"@api-platform/api-doc-parser": "0.8.2",
"@fortawesome/fontawesome-svg-core": "^1.2.28",
"@fortawesome/free-solid-svg-icons": "^5.13.0",
"@fortawesome/react-fontawesome": "^0.1.9",
"@material-ui/core": "^4.9.10",
"@material-ui/icons": "^4.9.1",
"@react-keycloak/web": "^2.1.1",
"@types/pluralize": "^0.0.29",
"google-geocoder": "0.2.1",
"history": "^4.10.1",
"keycloak-js": "^9.0.3",
"lodash.debounce": "^4.0.8",
"lodash.omit": "^4.5.0",
"lodash.set": "4.3.2",
"notistack": "0.9.9",
"papaparse": "^5.2.0",
"parcel-bundler": "1.12.4",
"polished": "^3.5.2",
"react": "16.13.1",
"react-admin": "3.4.1",
"react-dom": "16.13.1",
"react-is": "16.13.1",
"react-redux": "^7.2.0",
"recompose": "^0.30.0",
"redux": "4.0.5",
"styled-components": "5.1.0"
},
"devDependencies": {
"@babel/core": "7.9.0",
"@babel/plugin-syntax-export-default-from": "7.8.3",
"@babel/preset-env": "7.9.5",
"@babel/preset-react": "7.9.4",
"@storybook/addon-a11y": "5.3.18",
"@storybook/addon-actions": "5.3.18",
"@storybook/addon-info": "5.3.18",
"@storybook/addon-knobs": "5.3.18",
"@storybook/addon-links": "5.3.18",
"@storybook/addon-storyshots": "5.3.18",
"@storybook/addon-storysource": "5.3.18",
"@storybook/addon-viewport": "5.3.18",
"@storybook/react": "5.3.18",
"@testing-library/react": "^10.0.3",
"@types/jsonld": "1.5.1",
"@types/lodash": "4.14.149",
"@types/node": "13.11.1",
"@types/papaparse": "5.0.3",
"@types/react-redux": "7.1.7",
"@types/recompose": "^0.30.7",
"@types/styled-components": "5.1.0",
"@welldone-software/why-did-you-render": "4.0.7",
"awesome-typescript-loader": "5.2.1",
"babel-loader": "^8.1.0",
"babel-plugin-module-resolver": "4.0.0",
"babel-plugin-styled-components": "1.10.7",
"lodash.get": "4.4.2",
"lodash.uniq": "4.5.0",
"microbundle": "0.11.0",
"openapi-types": "1.3.5",
"parcel-plugin-static-files-copy": "2.3.1",
"pluralize": "^8.0.0"
},
"alias": {
"jsonld": "./node_modules/jsonld/dist/jsonld.js"
},
"staticFiles": {
"staticPath": "public",
"watcherGlob": "**"
}
}
同样值得注意的是,这只是React我遇到的问题。我所有其他经过重组的进口商品都可以正常工作。
global
标志--globals react=React
添加为对等依赖项吗?<-尽管可能不是适当的解决方法。看一下这个问题:github.com/developit/microbundle/issues/537它看起来像来自yarn
microbundler
而不是react-scripts
进行生产构建,或者某种方式以错误的方式更改了捆绑程序配置。我想引起您的注意,react hooks的名称应该以开头,use
并且可能在这行中import ue,{useEffect as pe,useState as fe}from"react";
,因为react pe
出了问题,所以导入了useEffect 。因此,您是否尝试过使用create-react-app
和构建react-scripts
?