0

Trying to implement a relative simple input form via using some Refs. Unfortunately I get always a "Module build failed: SyntaxError: Unexpected token" error.

Tried to do it how it is recommended in React 16.3+ with React.createRef() as well as the old way with a ref={(input) => this.title = input} inline of the input.


Here is the component for option 1:

import React from 'react';

class AddTextForm extends React.Component {

submit = () => {
  console.log(this.title.value)
  }

  render () {
    return (
      <div>
        <form>

          <input name="title" ref={(input) => this.title = input} type="text" placeholder="Add title" />   
          <button type="submit" onClick={this.submit} className="detailsctabutton w-button">Add Text</button>
        </form>
      </div>
    );
  }
}
export default AddTextForm;

And this is the error:

/src/components/AddTextForm.js
Module build failed: SyntaxError: Unexpected token (5:7)

  3 | class AddTextForm extends React.Component {
  4 | 
> 5 | submit = () => {
    |        ^
  6 |   console.log(this.title.value)
  7 |   }
  8 | 

Here is the component option 2:

import React from 'react';

class AddTextForm extends React.Component {

  titleRef = React.createRef();

  createText (event) {
    event.preventDefault();
    const text = {
      title: this.titleRef.value.value
    };
    console.log(text);
  };

  render () {
    return (
      <div>
        <form onSubmit={this.createText}>

          <input name="title" ref={this.titleRef} type="text" placeholder="Add title" />

          <button type="submit" className="detailsctabutton w-button">Add Text</button>
        </form>
      </div>
    );
  }
}
export default AddTextForm;

and this is the error for option 2:

./src/components/AddTextForm.js
Module build failed: SyntaxError: Unexpected token (5:11)

  3 | class AddTextForm extends React.Component {
  4 | 
> 5 |   titleRef = React.createRef();
    |            ^
  6 | 
  7 |   createText (event) {
  8 |     event.preventDefault();

The code that I try to implement is also part of a tutorial, so that should obviously work, but I have no clue why not in this case.

Is this an issues with my setup? Webpack? (sorry, newbie to Webpack)

Here is the webpack.config.js

const path = require('path');
const webpack = require('webpack');

// copy manifest.json to the path: 'public/build'
// this will allow for the authRequest to see the file at www.example.com/manifest.json
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ManifestAssetPlugin = new CopyWebpackPlugin([ { from: 'src/assets/manifest.json', to: 'manifest.json' } ]);
const IconAssetPlugin = new CopyWebpackPlugin([ { from: 'src/images/icon-192x192.png', to: 'icon-192x192.png' } ]);
const UglifyEsPlugin = require('uglify-es-webpack-plugin');
const UglifyEsPluginConfig = new UglifyEsPlugin({
    mangle: {
        reserved: [
                    'Buffer',
                        'BigInteger',
                        'Point',
                        'ECPubKey',
                        'ECKey',
                        'sha512_asm',
                        'asm',
                        'ECPair',
                        'HDNode'
                ]
        }
})


const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: './src/index.js',
  target: 'web',
  output: {
    path: path.resolve('public/build'),
    filename: 'index_bundle.js',
  },
  devServer: {
    historyApiFallback: {
      disableDotRule: true
    },
    watchOptions: { aggregateTimeout: 300, poll: 1000 },
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
      "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization",
    },
  },
  module: {
    rules: [
      { test: /\.json$/, use: 'json-loader' },
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)(\?\S*)?$/,
        loader: 'file-loader!url-loader',
      },
      { test: /\.css$/, loader: 'style-loader!css-loader' }
    ]
  },
  plugins: [
    HtmlWebpackPluginConfig, 
    ManifestAssetPlugin, 
    IconAssetPlugin,
    UglifyEsPluginConfig
 ]
}

Here are the dependencies:

"devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "file-loader": "^0.11.1",
    "html-webpack-plugin": "^2.28.0",
    "json-loader": "^0.5.4",
    "path": "^0.12.7",
    "style-loader": "^0.16.1",
    "uglify-es-webpack-plugin": "^0.10.0",
    "url-loader": "^0.5.8",
    "webpack": "^2.3.3",
    "webpack-dev-server": "^2.4.2"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "react": "^16.4.0",
    "react-dom": "^16.4.0",
    "react-router-dom": "^4.2.2"
  }
}

Thanks for helping out! Drives me crazy for two days.

YvonC
  • 329
  • 2
  • 6
  • 22
  • Aren't you targetting the wrong babel version? I mean it seems you want to use class instance fields, and I'm not sure from on which version this is actually working – Icepickle Jun 14 '18 at 08:34
  • 2
    Check this https://stackoverflow.com/questions/48801984/unexpected-token-in-react-component/48802029#48802029 – Shubham Khatri Jun 14 '18 at 08:37
  • Thanks so much @ShubhamKhatri ! Hero! Sorry, for an embarrassing follow up question: I installed `npm install --save-dev babel-plugin-transform-class-properties` -> and I added it in the .babelrc as a plugin ... and indeed the "unexpected token error" went away THANKS! Though in the option 2 above (the React.createRef(); case ) I get now a "Type Error": `TypeError: undefined is not an object (evaluating 'this.titleRef')`- Is this related or a separate issue? – YvonC Jun 14 '18 at 09:08
  • 1
    This is a separate issue, related to how you are using ref in a function which is not binded, not with babel. This question will help you solve it https://stackoverflow.com/questions/39503559/uncaught-typeerror-cannot-read-property-state-or-props-of-undefined/39503728#39503728, Basically you need to bind the `createText` function – Shubham Khatri Jun 14 '18 at 12:50

0 Answers0