提问者:小点点

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


在开发环境中,我的应用程序工作正常。在正式生产环境中,它会出现以下错误:

未捕获的类型错误:(0,_react.use效果)不是一个函数

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

import React, { useEffect } from 'react'

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

  ...
}

添加控制台。这一行下方的日志确认了useEffect在生产中确实是未定义的,而在开发中则是未定义的。

我package.jsonyarn.lock

我试图添加和删除它从对等依赖没有成功。

我输入了一个检查,以确保没有两个独立版本的React运行,但保存了窗口。React1=React在库和窗口中。React2=React在我的应用程序和检查

窗口。React1===窗口。React2这是真的,所以也不是这样。

最后,我还尝试对node_模块中的特定别名做出反应,但没有任何运气。

我找到的唯一解决方案是,如果我像这样导入它:

import React from 'react';

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

但这应该与使用非结构化导入完全相同?如果我明确使用React。useEffect它还强制我将所有其他useState和useEffect挂钩更改为React。使用sate反应。使用效果

下一个错误变成:TypeError:(0,\u react.useState)不是我使用react钩子的另一个文件中的函数。

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

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

我使用的捆绑版本是捆绑的。mjs

我检查了缩小后的图像的输出。mjs捆绑包和内部React的导入方式如下:

import ue,{useffect作为pe,useState作为fe}来自“react”

我觉得很好。

我真正不明白的是重组后的导入会如何打破它,但是仅仅做eact.use效果就可以了?

这是我的包裹。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": "**"
  }
}

同样值得注意的是,这是我唯一的问题。我所有其他的重组进口都很好。


共2个答案

匿名用户

似乎微起绒机不能容忍反应。这一个创建了试图从全局范围使用react的捆绑包,而不是真正公开的react

出于同样的原因,您的React.use效果的工作方法与预期的相同,只需想象它看起来像窗口。React.use效应。

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

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正确建议的那样,它将生成正确的包。我希望评论能解释发生了什么。

!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>

匿名用户

你可以通过做以下事情来修复它:

import React, { useState, useEffect } from "react";