简体   繁体   中英

React: ReferenceError: regeneratorRuntime is not defined

I am trying to use async and await in my react application.

   onSubmit = async (model) => {
        await this.setState({ data: model });
    }

After adding the above code i get an error in my browser console.

ReferenceError: regeneratorRuntime is not defined

.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": [
        "@babel/plugin-proposal-class-properties"
    ],
    "sourceMaps": "inline"

}

webpack.config.js

const path = require("path");
const WebpackShellPlugin = require("webpack-shell-plugin");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = [
    {
    Server config removed

 },

    {
        entry: {
            app1: './src/public/app1/index.js',
            app2: './src/public/app2/index.js',
            app3: './src/public/app3/index.js',
        },
        devtool: "source-map",
        output: {
            path: __dirname + '/dist/public/',
            publicPath: '/',
            filename: '[name]/bundle.js',
            devtoolLineToLine: true,
            sourceMapFilename: "[name]/bundle.js.map",

        },
        module: {
            rules: [
                {
                    test: /(\.css|.scss)$/,
                    use: [{
                        loader: "style-loader" // creates style nodes from JS strings
                    }, {
                        loader: "css-loader" // translates CSS into CommonJS
                    }, {
                        loader: "sass-loader" // compiles Sass to CSS
                    }]
                },
                {
                    test: /\.(jsx|js)?$/,
                    use: [{
                        loader: "babel-loader",
                        // options: {
                        //     cacheDirectory: true,
                        //     presets: ['react', 'es2015'] // Transpiles JSX and ES6
                        // }
                    }]
                }
            ],

        },
        "plugins": [
            new CopyWebpackPlugin([
                {
                    from: 'src/public/app1/index.html',
                    to: 'app1'
                },
                {
                    from: 'src/public/app2/index.html',
                    to: 'app2'
                },
                {
                    from: 'src/public/app3/index.html',
                    to: 'app3'
                },
            ]),

        ]

    }
];

I have added my babelrc and webpack config. Please let me know if i am missing something that would cause this error to appear in my browser console.

Import regeneratorRuntime in the component using async/await:

import regeneratorRuntime from "regenerator-runtime";

*** UPDATED ANSWER *** (probably don't use above)

Import babel and @babel/plugin-transform-runtime plugin:

package.json

  "devDependencies": {
    "@babel/core": "^7.8.7",
    "@babel/plugin-transform-runtime": "^7.8.3",
  },

.babelrc

{
  "plugins": ["@babel/plugin-transform-runtime"]
}

You did not include your package.json file, so it is a bit unclear what you are missing.

Assuming you have @babel/polyfill as a dependency in your package.json file, is it possible that you are not specifying:

import '@babel/polyfill'

in your React file (such as index.js)?

Adding polyfills from create-react-app worked for me.

yarn add --dev react-app-polyfill

Then add the following lines to webpack.config.js

entry: {
  app: [
    'react-app-polyfill/ie9', // Only if you want to support IE 9
    'react-app-polyfill/stable',
    './src/index.jsx',
  ],
},

See more examples on the react-app-polyfill GitHub page .

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