TypeError: (0 , _react.useEffect) 不是函数

MLy*_*yck 15 javascript reactjs parceljs react-hooks

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

Uncaught TypeError: (0 , _react.useEffect) is not a function

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

import React, { useEffect } from 'react'

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

  ...
}

Run Code Online (Sandbox Code Playgroud)

在此行下方添加一个 console.log 确认 useEffect 在生产中确实未定义,而在 dev 中则是预期的功能。

我检查了我的 package.json、yarn.lock 和 node_modules 是否有任何可能在 16.8.0 下引入 useEffect 的 react 或 react-dom 版本。但一切都是 16.13.1,它们是主要依赖项,我确实尝试清理我的纱线缓存,删除 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(() => { ... })
  ...
}
Run Code Online (Sandbox Code Playgroud)

但这应该和使用解构导入完全一样吧?如果我明确使用 React.useEffect,它也会强制我将所有其他 useState 和 useEffect 挂钩更改为React.useSateReact.useEffect

下一个错误就变成了:TypeError: (0 , _react.useState) is not a function在我使用 React 钩子的另一个文件中。

我想解决问题而不是实施变通方法。

我曾经使用microbundleReact 捆绑我的库。我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": "**"
  }
}

Run Code Online (Sandbox Code Playgroud)

同样值得注意的是,只有 React 我遇到了这个问题。我所有其他重组的进口工作都很好。

Kyr*_*Kyr 7

似乎microbundler不能容忍 React。这个创建尝试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'));
Run Code Online (Sandbox Code Playgroud)

捆绑后microbundle它完全坏了,但是当你尝试捆绑时

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'));
Run Code Online (Sandbox Code Playgroud)

正如正确建议@Jee Mok,它将产生正确的包。我希望评论能解释发生了什么。

microbundle --globals react=React
Run Code Online (Sandbox Code Playgroud)
!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"));
});
Run Code Online (Sandbox Code Playgroud)

而且,顺便说一句,“重组进口”根本不是罪魁祸首。


小智 5

您可以通过执行以下操作来修复它:

import React, { useState, useEffect } from "react";
Run Code Online (Sandbox Code Playgroud)