reactjs意外的令牌'<'

IT技术 javascript reactjs
2021-05-14 21:24:41

我用 react-redux,redux 创建了 reactjs 应用程序。启动 reactjs 项目时,在索引文件的第 13 行出现意外的令牌错误。下面给出了索引文件。

ERROR in ./src/index.js
Module build failed: SyntaxError: D:/Reactjs_work/
/src/index.js: Unexpected token (13:2)

  11 |
  12 | ReactDOM.render(
> 13 |   <Provider store={createStoreWithMiddleware(reducers)}>
     |   ^
  14 |     <App />
  15 |   </Provider>
  16 |   , document.querySelector('.container'));

 @ multi main
webpack: Failed to compile.

以下是 index.js 文件

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';

import App from './components/app';
import reducers from './reducers';

const createStoreWithMiddleware = applyMiddleware()(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <App />
  </Provider>
  , document.querySelector('.container'));

我已经完成了以下主题here.but没有成功。 Reactjs:意外标记“<”错误 ReactJS 语法错误:意外标记

webpack.config.js

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel'
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};
2个回答

用以下代码替换module

module: {
    loaders: [{
      test: /\.(js|jsx)?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',

        query: {
           presets: ['es2015', 'react']
        }
    }]
  },

有同样的错误:

ERROR in ./src/main.js
Module build failed: SyntaxError: Unexpected token (11:1)

   9 | 
  10 | ReactDOM.render(
> 11 |  <Provider store={store}>
     |  ^
  12 |      <App />
  13 |  </Provider>,
  14 |  mountNode

 @ multi main
webpack: Failed to compile.

我不帮忙更改module部分。

我的错误:缺少 .babelrc 文件

使用内容创建 .babelrc 后:

{
    "presets": ["react", "es2015", "stage-0"]
}

我的错误消失了。很简单 :-)