简体   繁体   English

TypeScript React Babel ReferenceError:找不到变量:regeneratorRuntime

[英]TypeScript React Babel ReferenceError: Can't find variable: regeneratorRuntime

I am learning typescript, react and babel.我正在学习打字稿、反应和 babel。 This is my code这是我的代码

export default function App() : JSX.Element {
    console.log('+++++ came inside App +++++++ ');
    const {state, dispatch} = useContext(Store);
    useEffect(() => {
        console.log('came inside use effect')
        state.episodes.length === 0 && fetchDataAction()
    });

    const fetchDataAction = async () => {
        console.log('came inside fetch data');
        const URL = 'https://api.tvmaze.com/singlesearch/shows?q=rick-&-morty&embed=episodes';
        const data = await fetch(URL);
        const dataJson = await data.json();
        return dispatch({
            type: 'FETCH_DATA',
            payload: dataJson._embedded.episodes
        })
    }
    return (
        <Fragment>
            <h1>Rick and Morty</h1>
            <p>Pick your favorite episode!!!</p>
            {console.log(state)}
        </Fragment>
    )
}

when I try to make a sync call from my component I get this error当我尝试从我的组件进行同步调用时出现此错误

ReferenceError: Can't find variable: regeneratorRuntime
(anonymous function) — App.tsx:29
App — App.tsx:63
renderWithHooks — react-dom.development.js:16260
mountIndeterminateComponent — react-dom.development.js:18794
callCallback — react-dom.development.js:336
dispatchEvent
invokeGuardedCallbackDev — react-dom.development.js:385
invokeGuardedCallback — react-dom.development.js:440
beginWork$$1 — react-dom.development.js:25780
performUnitOfWork — react-dom.development.js:24698
workLoopSync — react-dom.development.js:24671
performSyncWorkOnRoot — react-dom.development.js:24270
scheduleUpdateOnFiber — react-dom.development.js:23698
updateContainer — react-dom.development.js:27103
(anonymous function) — react-dom.development.js:27528
unbatchedUpdates — react-dom.development.js:24433
legacyRenderSubtreeIntoContainer — react-dom.development.js:27527
Eval Code — index.tsx:13
eval
./src/index.tsx — bundle.js:613
__webpack_require__ — bundle.js:20
Eval Code — client:2
eval
(anonymous function) — bundle.js:624
__webpack_require__ — bundle.js:20
(anonymous function) — bundle.js:84
Global Code — bundle.js:85
logCapturedError — react-dom.development.js:21843

I googled and found this thread Async/Await ReferenceError: Can't find variable: regeneratorRuntime我用谷歌搜索并找到了这个线程Async/Await ReferenceError: Can't find variable: regeneratorRuntime

but I still don't know what exactly did the person do to solve the problem.但我仍然不知道这个人究竟做了什么来解决问题。 Is it possible for someone to explain the solution a little more to me.是否有人可以向我解释一下解决方案。 I am not aware of what polyfills is.我不知道 polyfills 是什么。

This is my whole code which shows my babel and webpack configs https://github.com/abhsrivastava/react-rick-and-morty这是我的全部代码,它显示了我的 babel 和 webpack 配置https://github.com/abhsrivastava/react-rick-and-morty

I kept googling and experimenting and I was able to resolve the problem.我一直在谷歌搜索和试验,我能够解决这个问题。 here is a more step by step answer.这是一个更一步一步的答案。

  1. yarn add @babel/runtime纱线添加@babel/runtime
  2. yarn add -D @babel/plugin-transform-runtime yarn add -D @babel/plugin-transform-runtime

Now make the following changes to the .babelrc file现在对.babelrc文件进行以下更改

{
    "presets": ["@babel/preset-env", "@babel/typescript", "@babel/react"],
    "plugins": [
        ["@babel/plugin-transform-runtime",
        {
          "regenerator": true
        }]
      ]
}

After making these changes, all the 4 problems were solved进行这些更改后,所有4个问题都解决了

  1. Babel ReferenceError: Can't find variable: regeneratorRuntime Babel ReferenceError:找不到变量:regeneratorRuntime
  2. Error: Cannot find module '@babel/runtime/helpers/slicedToArray'错误:找不到模块“@babel/runtime/helpers/slicedToArray”
  3. Module build failed: TypeError: this.setDynamic is not a function模块构建失败:类型错误:this.setDynamic 不是函数
  4. The 'polyfill' option has been removed. 'polyfill' 选项已被删除。 The @babel/runtime module now skips polyfilling by default. @babel/runtime 模块现在默认跳过 polyfill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到变量:regeneratorRuntime (ReactNative) - Can't find variable: regeneratorRuntime (ReactNative) Babel 7 - 未捕获的 ReferenceError:未定义 regeneratorRuntime - Babel 7 - Uncaught ReferenceError: regeneratorRuntime is not defined 反应:ReferenceError:regeneratorRuntime 未定义 - React: ReferenceError: regeneratorRuntime is not defined ReferenceError: 找不到变量: False - React Native (Hookstate) - ReferenceError: Can't find variable: False - React Native (Hookstate) REACT-NATIVE - abcjs - ReferenceError:找不到变量:元素 - REACT-NATIVE - abcjs - ReferenceError: Can't find variable: Element 未捕获的 ReferenceError:RegeneratorRuntime 未在 React 中定义 - Uncaught ReferenceError: regeneratorRuntime is not defined in React 获取ReferenceError:找不到变量:是否需要webpack? - Getting ReferenceError: Can't find variable: require react webpack? ReferenceError:找不到变量:loadMoviesLikedDetails(React Native) - ReferenceError: Can't find variable: loadMoviesLikedDetails (React Native) React Navigation ReferenceError:找不到变量:isSelectionModeEnabled - React Navigation ReferenceError: Can't find variable: isSelectionModeEnabled 安装 React 时出现 React Native 错误(ReferenceError:找不到变量:React)? - React Native Error (ReferenceError: Can't find variable: React) when React is installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM