Skip to content

React w/ Webpacker - SyntaxError: Unexpected token #711

Description

@kyleknighted

Help us help you! Please choose one:

  • My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.

Getting this error on a simple React component.

  • Rails 5.1
  • react-rails
  • webpacker
/app/javascript/components/Host.es6.jsx
Module build failed: SyntaxError: Unexpected token (4:16)

  2 | 
  3 | class Host extends Component {
> 4 |   renderHostRow = (host) => {
    |                 ^
  5 |     return (
  6 |       <tr>
  7 |         <td></td>

 @ ./app/javascript/components ^\.\/.*$
 @ ./app/javascript/packs/application.js
 @ multi (webpack)-dev-server/client?https://localhost:8080 ./app/javascript/packs/application.js

I updated /config/webpack/loaders/react.js to include React and ES2015 presets

module.exports = {
  test: /\.(js|jsx)?(\.erb)?$/,
  exclude: /node_modules/,
  loader: 'babel-loader',
  query: {
    presets: ['react', 'es2015']
  }
}

The actual component code is pretty straightforward
This is a component I simplified that works in a straight React application.
I removed the PropTypes, states, etc, and just got it down to simply render a table.
It still fails.

import React, { Component, PropTypes } from 'react';

class Host extends Component {
  renderHostRow = (host) => {
    return (
      <tr>
        <td>..hostname..</td>
        <td>..user..</td>
        <td>macOS {host.os_name}</td>
        <td>{host.hardware_serial}</td>
      </tr>
    );
  }

  render() {
    const { renderHostRow } = this;
    const { hosts } = this.props;

    return (
      <table>
        <thead>
          <tr>
            <th>Hostname</th>
            <th>User</th>
            <th>OS</th>
            <th>Serial #</th>
          </tr>
        </thead>
        <tbody>
          {hosts.map((host) => {
            return renderHostRow(host);
          })}
        </tbody>
      </table>
    );
  }
}

export default Host;

Any ideas would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions