gpt4 book ai didi

javascript - TypeError : (0 , _react.useEffect) 不是一个函数

转载 作者:行者123 更新时间:2023-12-03 13:20:38 30 4
gpt4 key购买 nike

在开发环境中,我的应用程序运行良好。在生产环境中它崩溃并出现错误:

未捕获类型错误:(0, _react.useEffect) 不是函数

它发生在我创建的文件中,我在其中导入 React 和 useEffect,如下所示:

import React, { useEffect } from 'react'

const X = () => {
useEffect(() => { ... })

...
}

在该行下方添加 console.log 可以确认 useEffect 在生产中确实未定义,而在开发中则为预期函数。

我检查了我的 package.json、yarn.lock 和 node_modules 中是否有任何可能低于引入 useEffect 的 16.8.0 版本的 React 或 React-dom 版本。但一切都是 16.13.1,它们是主要依赖项,我确实尝试清理我的 yarn 缓存,删除node_modules和yarn.lock,然后重新安装。

我尝试从 peerDependencies 添加和删除它,但没有成功。

我进行了检查,以确保没有 2 个独立版本的 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 Hook 更改为 React.useSateReact.useEffect

下一个错误变成:TypeError: (0, _react.useState) is not a function in another file where I use React hooks.

我想解决问题而不是实现解决方法。

我使用 microbundle 使用 React 来捆绑我的库。我使用 parcel-bundler 导入 React 组件并将其渲染在开发环境(直接来自 src)或 prod(捆绑库)中

我使用的捆绑版本是与.mjs捆绑的

我还检查了缩小的 .mjs 包的输出,React 内部的导入如下:

导入 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 遇到这个问题。我所有其他重组的导入工作都很好。

最佳答案

看来 microbundler 不能容忍 React。这个创建的 bundle 尝试在全局范围内使用 react,而不是真正暴露的 React

出于同样的原因,您使用 React.useEffect 的解决方法可以按预期工作,想象一下它看起来像 window.React.useEffect

这是一个原始应用程序的示例:

import ReactDOM from 'react-dom';
import React, { useEffect, useState } from 'react';

/**
* necessary workaround, microbundle use `h` pragma by default,
* that undefined when use React
* another option is to make build with option --jsx
* @example microbundle --globals react=React --jsx React.createElement
* yes, yet another workaround
*/
window.h = React.createElement;

const X = () => {
const [A, B] = useState('world');

useEffect(() => {
B('MLyck');
}, [])

return `Hello ${A}`;
}

ReactDOM.render(<X />, document.querySelector('react-app'));

仅与 microbundle 捆绑后,它完全损坏了,但是当您尝试与

捆绑时
microbundle --globals react=React

按照 @Jee Mok 的正确建议,它将生成正确的 bundle 。我希望评论能解释发生了什么。

!function (e, t) {
"object" == typeof exports && "undefined" != typeof module ?
t(require("react-dom"), require("react")) :
"function" == typeof define && define.amd ?
define(["react-dom", "react"], t) :
t(e.ReactDOM, e.React);
/*
String above is core of problem,
in case you try to bundle without options `--globals react=React`
it will looks like: `t(e.ReactDOM, e.react);`
Obviously `react` is not defined in `e` e.g. `this` e.g. `window`
due to react expose self as `React`
*/
}(this, function (e, t) {
e = e && e.hasOwnProperty("default") ? e.default : e, window.h = ("default" in t ? t.default : t).createElement, e.render(h(function () {
var e = t.useState("world"), n = e[0], r = e[1];
return t.useEffect(function () {
r("MLyck");
}, []), "Hello " + n;
}, null), document.querySelector("react-app"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.development.js"></script>

<react-app></react-app>

顺便说一句,“重组进口”根本不应该受到指责。

关于javascript - TypeError : (0 , _react.useEffect) 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61348714/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com