0

I have a react component that works without arrow functions (commented out below), but I would love being able to use arrow functions instead of binding each function by hand.

This (the not commented out part) is what I tried:

import React from 'react'

export default class CowClicker extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      clicks: 0
    }
    // this.onCowClick = this.onCowClick.bind(this) 
    // that makes it work if I use a normal function instead of a fat arrow one for onCowClick
  }

  // alternative that works: onCowClick(evt) {...}
  onCowClick = (evt) => {
    this.setState({
      clicks: this.state.clicks + 1
    })
  }

  render() {
    return (
      <div>
        <div>Clicks: {this.state.clicks}</div>
        <img
          src="http://s3.bypaulshen.com/buildwithreact/cow.png"
          onClick={this.onCowClick}
          className="cow"
          />
        <p>Click the cow</p>
      </div>
    )
  }
}

How can I enable the use of arrow functions in my project ?

This link seems to be part of the solution, but as a front-end newbie I fail to know how to apply it.

Also, as a side question, how do you know what is wrong in a jsx file ? What are good tools to troubleshoot react code ? I picked brunch as a build system because it seemed the simplest to get started with front-end, and the only error it gives me is Compiling of app/components/CowClicker.jsx failed.


For more context :

  • my app is set up using brunch new -s react, which puts a react app skeleton in place
  • I'm using vscode as a text editor if you have any linting / compilation error spotting extensions to recommend, I would definitely appreciate
  • My build config (I build with brunch) looks like this (it's the default one with the 'react' skeleton of brunch) :

.

module.exports = {
  files: {
    javascripts: {
      joinTo: {
        'vendor.js': /^(?!app)/,
        'app.js': /^app/
      }
    },
    stylesheets: {joinTo: 'app.css'}
  },

  plugins: {
    babel: {presets: ['es2015', 'react']}
  }
};
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
  • The line you commented out is correct btw - What line is the error? I don't quite understand your question – curv Nov 28 '16 at 18:06
  • 2
    `onCowClick = (evt) => {` in a class is an experimental syntax proposal, if you're using Babel you'll need to explicitly enable it. – loganfsmyth Nov 28 '16 at 18:07
  • *"that makes it work if I use a normal function instead of a fat arrow one"* You cannot bind the `this` value of arrow functions. – Felix Kling Nov 28 '16 at 18:34
  • Another alternative would be `::onCowClick` using https://babeljs.io/docs/plugins/transform-function-bind/ – Dominic Nov 28 '16 at 19:02
  • @zinc How can I know on what line my error is ? I'm very interested in knowing that – Nicolas Marshall Nov 28 '16 at 19:39
  • How are you compiling it? What's your build pipeline looking like? – curv Nov 28 '16 at 19:44
  • @zinc I included my build config in the question – Nicolas Marshall Nov 28 '16 at 20:01
  • Note that using function properties is convenient, but they're created per-instance. This often doesn't matter, e.g., components that appear on the page once, but if the component has hundreds of instances, it may be undesirable. – Dave Newton Nov 28 '16 at 20:03
  • @loganfsmyth How can I enable that ? I put a link in my question that tells me to add `"optional": ["es7.classProperties"]` in a config, but I have no idea in what config file – Nicolas Marshall Nov 28 '16 at 20:25
  • @FelixKling I actually meant I'm trying to use fat arrows instead of binding manually. I edited the question to try to make that clearer – Nicolas Marshall Nov 28 '16 at 20:27
  • @DaveNewton What is the alternative in my case ? Where can I learn about that ? – Nicolas Marshall Nov 28 '16 at 20:28
  • @n-marshall You are looking for https://babeljs.io/docs/plugins/transform-class-properties/ – loganfsmyth Nov 28 '16 at 20:38
  • @n-marshall Not sure what you're asking. In any case you need the stage-n preset that include class properties. I forget which one it's in, but each stage documents what's included. – Dave Newton Nov 28 '16 at 20:38

0 Answers0