This doesn't have anything to do with arrow functions. You're trying to assign a value to an identifier you haven't declared anywhere.
This:
goBack = () => {
// do stuff
}
assigns an arrow function to the already declared identifier goBack
. (Or, if this were within a class
, it would create a new property and assigns the arrow function to it — but we know you're not doing this in a class, beacuse your const
version would fail if you were.)
It's exactly like:
answer = 42;
If answer
isn't declared, you get an error from TypeScript.
This:
const goBack = () => {
// do stuff
}
creates a local variable (well, constant) and assigns the arrow function to it, just like:
const answer = 42;
It's useful to remember that arrow functions have no declaration syntax. An arrow function is always an expression. The part to the left of the =
in your examples isn't part of that arrow function expression (although, somewhat surprisingly, it can have an effect on the function that's created).
goBack
defined somewhere? That is what you are trying to assign a value to. If not you need a local one and you do that with either let/var/const. This is not just an arrow function, it would also have this error if you did a simple string assignment. – Cicely