首页 > 解决方案 > 使用情绪/反应的组件包时出现运行时错误“对象(...)不是函数”

问题描述

我有一个引入了@emotion/react@v11.x 的组件库。我现在正在尝试升级我的 ui 项目以使用该新包,但我遇到了一个运行时错误,该错误似乎与情绪如何消耗反应有关。

emotion-element-4fbd89c5.browser.esm.js:28 Uncaught TypeError: Object(...) is not a function
    at eval (emotion-element-4fbd89c5.browser.esm.js:28)
    at Object.../node_modules/@emotion/react/dist/emotion-element-4fbd89c5.browser.esm.js (index.js:944)
    at __webpack_require__ (index.js:753)
    at fn (index.js:122)
    at Object.eval (emotion-react.browser.esm.js:10)
    at eval (emotion-react.browser.esm.js:362)
    at Object.../node_modules/@emotion/react/dist/emotion-react.browser.esm.js (index.js:956)
    at __webpack_require__ (index.js:753)
    at fn (index.js:122)
    (truncated)

经过进一步检查,问题似乎是解决 react 'createContext' 函数的问题。

var EmotionCacheContext = /* #__PURE__ */Object(react__WEBPACK_IMPORTED_MODULE_0__["createContext"])( // we're doing this to avoid preconstruct's dead code elimination in this one case

组件库使用 react v16.8.1,UI 库使用相同版本。

任何想法或指示将不胜感激!

标签: reactjsemotion

解决方案


我遇到了同样的问题,对我有用的是删除我的锁定文件和节点模块,然后运行yarnnpm install. 之后,我在浏览器中做了一个空缓存并硬重新加载。我也跑了gatsby clean,因为我正在使用 Gatsby。我不确定哪一步成功了,但这对我有帮助!根据您的问题,我猜您正在迁移到 Chakra UI v1?如果是这种情况,请确保您在此处关注他们的迁移文档。


推荐阅读