0

I'm currently using React and react-bootstrap-tables in my project and the documentation instructs me to use the following method:

createCustomExportCSVButton = (onClick) => {
  return (
    <ExportCSVButton 
      className="action button-gray smaller right"
      btnText="Export CSV 2"
    />
  );
}

However, I am getting an an unexpected token error:

ERROR in ./project/web/frontend/static/js/CyhyTable.js
Module build failed: SyntaxError: Unexpected token (67:29)

  65 |  }
  66 | 
> 67 |  createCustomExportCSVButton = (onClick) => {
     |                              ^
  68 |      return (
  69 |          <ExportCSVButton 
  70 |              className="action button-gray smaller right"

I'm not familiar with this syntax, and everytime I try and google it the query seems to ignore the '=>' part so im not really sure what to call this type of function. I'm using webpack to compile the react frontend assets, could it be a problem with my webpack file?

var webpack = require('webpack');

module.exports = {  
  entry: [
    "./project/web/frontend/static/bower_components/jquery/dist/jquery.min.js",
    "./project/web/frontend/static/js/app.js",

    "./project/web/frontend/static/js/flakes/base.js", 

  ],
  output: {
    path: __dirname + '/project/web/frontend/static/js',
    filename: "bundle.js"
  },
  module: {
    loaders: [
      {
        test: /\.js?$/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react']
        },   
        exclude: /node_modules/,
      },
      {test: /\.jsx$/, loader: 'jsx-loader'},

    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
    })  
  ]
};

Here is the full CyhyTable JSX:

/* eslint max-len: 0 */
/* eslint no-unused-vars: 0 */
/* eslint no-alert: 0 */
var React = require('react');
var ReactDOM = require('react-dom');
var ReactBsTable  = require('react-bootstrap-table');
var BootstrapTable = ReactBsTable.BootstrapTable;
var TableHeaderColumn = ReactBsTable.TableHeaderColumn;



var rowsSelected = []

function onRowSelect(row, isSelected, e) {
  let rowStr = '';
  for(const prop in row) {
    rowStr += prop + ': "' + row[prop] + "'";
  }
  rowsSelected.push(row);
  console.log(rowsSelected);
}

class CyhyTableSearch extends React.Component {

  getValue() {
    return ReactDOM.findDOMNode(this).value;
  }

  setValue(value) {
    ReactDOM.findDOMNode(this).value = value;
  }

  render() {
    return (

      <div className="flakes-search">
        <input 
          ref="searchInput"
          className="search-box"
          placeholder={ "Enter Value for Search.." } 
          defaultValue={ this.props.defaultValue } 
          onKeyUp={ this.props.search } />

        <div className="flakes-actions-bar">
          <button className="action button-gray smaller right"> DDI Selected</button>
          <button name="csvExport" className="action button-gray smaller right">Export CSV</button>
        </div>
      </div>
    );
  }
}



class CyhyTable extends React.Component {

  csvFormatter(cell, row) {
    return `${row.id}: ${cell} USD`;
  }

  renderShowsTotal(start, to, total) {
    return (
      <div className="flakes-pagination-right"></div>
    );
  }

  createCustomExportCSVButton = (onClick) => {
    return (
      <ExportCSVButton 
        className="action button-gray smaller right"
        btnText="Export CSV 2"
      />
    );
  }

  onExportToCSV(row) {
    return rowsSelected;
  } 

  render() {
    const selectRowProp = {
      mode: 'checkbox',
      onSelect: onRowSelect
    };

    const options = {
      clearSearch: false,
      searchPanel: (props) => (<CyhyTableSearch { ...props }/>),
      page: 1,  // which page you want to show as default
      sizePerPage: 25,  // which size per page you want to locate as default
      pageStartIndex: 0, // where to start counting the pages
      paginationSize: 3,  // the pagination bar size.
      prePage: 'Prev', // Previous page button text
      nextPage: 'Next', // Next page button text
      firstPage: 'First', // First page button text
      lastPage: 'Last', // Last page button text
      onExportToCSV: this.onExportToCSV,
      exportCSVText: 'test',
      exportCSVBtn: this.createCustomExportCSVButton,
      paginationShowsTotal: this.renderShowsTotal,
      sizePerPageList: [ {
      text: '5', value: 5
      }, {
      text: '10', value: 10
      }, {    
      text: '15', value: 15
      }, {
      text: '25', value: 25
      }, {
      text: '50', value: 50
      }, {
      text: '100', value: 100
      }, {
      text: 'All', value: cyhyData.length
      } ],      
    };

    return (  
      <BootstrapTable
        data={ cyhyData }
        options={ options }
        selectRow={ selectRowProp }
        exportCSV={ true }
        pagination={ true }
        tableHeaderClass='flakes-table'
        tableBodyClass='flakes-table'
        containerClass='flakes-table'
        tableContainerClass='flakes-table'
        headerContainerClass='flakes-table'
        bodyContainerClass='flakes-table' 
        search >

        <TableHeaderColumn isKey={true} dataField='id' hidden={true}>id</TableHeaderColumn>
        <TableHeaderColumn dataField='Severity'>Severity</TableHeaderColumn>
        <TableHeaderColumn dataField='IP'>IP</TableHeaderColumn>
        <TableHeaderColumn dataField='Port'>Port</TableHeaderColumn>
        <TableHeaderColumn dataField='DNS'>DNS</TableHeaderColumn>
        <TableHeaderColumn dataField='vulnName'>Vulnerability</TableHeaderColumn>
      </BootstrapTable>
    );
  }
}

module.exports = CyhyTable;
halfer
  • 19,824
  • 17
  • 99
  • 186
rogueSF
  • 87
  • 5

2 Answers2

1

You need to update your babel presets to be able to use that feature.

change 'es2015' to 'latest' as well as add 'stage-2' to the babel presets. in your webpack config under your babel loader. This will ensure you are getting the right transpilation features to make this work. I think stage-2 is the only one you need for this instance but if you plan on using the most up to date ES* then latest will pull that in for you.

  presets: [
          'stage-2',
          'latest',
          'react'
        ]
finalfreq
  • 6,830
  • 2
  • 27
  • 28
  • Aren't class properties in the [stage 2](http://babeljs.io/docs/plugins/preset-stage-2/) preset rather than latest? – Joe Clay Dec 06 '16 at 16:51
  • @JoeClay is correct need stage-2, presets: [ 'stage-2', // used for object property spread 'latest', // contains official es2015, es2016, es2017 presets 'react' ] is what we use in our app and works great – finalfreq Dec 06 '16 at 16:54
  • @finalfreq: Yep, that's the config I tend to use too :) – Joe Clay Dec 06 '16 at 16:56
  • Thanks for the response, I'm now getting the following error: ERROR in ./project/web/frontend/static/js/app.js Module build failed: Error: Couldn't find preset "stage-2" relative to directory at Code/Python3/cyreact/node_modules/babel-core/lib/transformation/file/options/option-manager.js:299:19 at Array.map (native) at OptionManager.resolvePresets (Code/Python3/cyreact/node_modules/babel-core/lib/transformation/file/options/option-manager.js:270:20) at OptionManager.mergePresets – rogueSF Dec 06 '16 at 16:59
  • Just ran npm install --save-dev babel-preset-stage-2..still wont build – rogueSF Dec 06 '16 at 17:01
  • what error are you getting after installing stage-2? – finalfreq Dec 06 '16 at 17:08
  • Thanks guys, got it working! Combined your solution with with some info off google and got the following to work: query: { presets: [require.resolve('babel-preset-es2015'), 'react', 'stage-2'], plugins: [require.resolve('babel-plugin-transform-runtime')] } – rogueSF Dec 06 '16 at 17:10
0

(Posted solution on behalf of the OP).

Needed to add the following to webpack:

presets: [require.resolve('babel-preset-es2015'), 'react', 'stage-2'],
plugins: [require.resolve('babel-plugin-transform-runtime')]
halfer
  • 19,824
  • 17
  • 99
  • 186