My question is very simple. If I have a class in ES6 is it possible to use an arrow function within it?
import React, { Component } from 'react';
export default class SearchForm extends Component {
state = {
searchText: ''
}
onSearchChange = e => {
this.setState({ searchText: e.target.value });
}
handleSubmit = e => {
e.preventDefault();
this.props.onSearch(this.query.value);
e.currentTarget.reset();
}
render() {
return (
<form className="search-form" onSubmit={this.handleSubmit} >
<label className="is-hidden" htmlFor="search">Search</label>
<input type="search"
onChange={this.onSearchChange}
name="search"
ref={(input) => this.query = input}
placeholder="Search..." />
<button type="submit" id="submit" className="search-button">
<i className="material-icons icn-search">search</i>
</button>
</form>
);
}
}
The reason I ask is that I get an error in my console, even when using Babel. It seems like there's a lot of resources on the internet stating you can do this (most of which are about developing with React).
Is this something that Babel should do, and will eventually become natively supported?
The error I get is an unexpected = sign, just before the parens.
EDIT: I forgot to mention, the reason I wish to do this is to make use of the this
keyword in context of the class. If I use a regular function - to my understanding - I would have to bind this
to the function. I'm trying to look for a nicer way of doing that.