I have a simple function (within a React component):
getInitialState: function() {
return {
text: this.props.text
}
}
But I want to fat arrowify it:
getInitialState: () => {
text: this.props.text
}
except I get an error, because a {
after a fat arrow means a block of code that returns undefined (unless you explicitly return
something). At least that's what I thought at first. But I think this
is being bound now to that fat arrow function and now this.props
is undefined
.
So I try this:
getInitialState: () => new Object({
text: this.props.text
})
But I get the same error, this.props
is undefined.
So I guess I have 2 issues I'm curious about. First, what's the idiomatic way to return an object from a simple statement fat arrow function? Second, how do I return an object that has a reference to the this
object of the surrounding context?