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

时间:2020-01-19 20:20:44

标签: reactjs typescript babeljs

我正在学习打字稿,反应和通天塔。这是我的代码

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

我用Google搜索并找到了该主题Async/Await ReferenceError: Can't find variable: regeneratorRuntime

但是我仍然不知道这个人到底做了什么来解决问题。有人可以向我解释更多解决方案吗?我不知道什么是polyfills。

这是我的整个代码,显示了我的babel和webpack配置https://github.com/abhsrivastava/react-rick-and-morty

1 个答案:

答案 0 :(得分:1)

我一直在谷歌搜索和试验,并且能够解决问题。这是一步一步的答案。

  1. 添加纱线@ babel / runtime
  2. 添加纱线-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. 模块构建失败:TypeError:this.setDynamic不是函数
  4. “ polyfill”选项已被删除。 @ babel / runtime模块现在默认情况下会跳过填充。