简体   繁体   中英

TypeScript React Babel ReferenceError: Can't find variable: regeneratorRuntime

I am learning typescript, react and 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

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.

This is my whole code which shows my babel and webpack configs 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
  2. yarn add -D @babel/plugin-transform-runtime

Now make the following changes to the .babelrc file

{
    "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

  1. Babel ReferenceError: Can't find variable: regeneratorRuntime
  2. Error: Cannot find module '@babel/runtime/helpers/slicedToArray'
  3. Module build failed: TypeError: this.setDynamic is not a function
  4. The 'polyfill' option has been removed. The @babel/runtime module now skips polyfilling by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM