簡體   English   中英

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

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

我正在學習打字稿、反應和 babel。 這是我的代碼

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>
    )
}

當我嘗試從我的組件進行同步調用時出現此錯誤

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

我用谷歌搜索並找到了這個線程Async/Await ReferenceError: Can't find variable: regeneratorRuntime

但我仍然不知道這個人究竟做了什么來解決問題。 是否有人可以向我解釋一下解決方案。 我不知道 polyfills 是什么。

這是我的全部代碼,它顯示了我的 babel 和 webpack 配置https://github.com/abhsrivastava/react-rick-and-morty

我一直在谷歌搜索和試驗,我能夠解決這個問題。 這是一個更一步一步的答案。

  1. 紗線添加@babel/runtime
  2. yarn add -D @babel/plugin-transform-runtime

現在對.babelrc文件進行以下更改

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

進行這些更改后,所有4個問題都解決了

  1. Babel ReferenceError:找不到變量:regeneratorRuntime
  2. 錯誤:找不到模塊“@babel/runtime/helpers/slicedToArray”
  3. 模塊構建失敗:類型錯誤:this.setDynamic 不是函數
  4. 'polyfill' 選項已被刪除。 @babel/runtime 模塊現在默認跳過 polyfill。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM