Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  37] [ 2]  / answers: 1 / hits: 22186  / 8 Years ago, wed, september 14, 2016, 12:00:00

I have this function in one of my react components.



export default class EventTags extends React.Component{
showAll () => {
this.setState({
showAll: true,
showBtn: false
});
}
}


When webpack watch hits it I get an unexpected token error on the arrow function. I have the transform-es2015-arrow-functions plugin enabled but it doesn't seem to change the outcome.



Any ideas on what i'm doing wrong here?


More From » reactjs

 Answers
11

You need an equals sign when using class property initializers.



export default class EventTags extends React.Component {
showAll = () => {
this.setState({
showAll: true,
showBtn: false
});
};
}



  • Ensure you have the transform-class-properties Babel transform enabled

  • Unlike class methods, class property initializers should be followed by semicolons



Babel's docs on arrow functions in ES6 React components shows longer examples.


[#60718] Monday, September 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;