[英]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
我一直在谷歌搜索和試驗,我能夠解決這個問題。 這是一個更一步一步的答案。
現在對.babelrc
文件進行以下更改
{
"presets": ["@babel/preset-env", "@babel/typescript", "@babel/react"],
"plugins": [
["@babel/plugin-transform-runtime",
{
"regenerator": true
}]
]
}
進行這些更改后,所有4個問題都解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.