What's the difference (if any) between the 2 below ways of defining a method inside a React component?
export default class Foo extends React.Component<
bar() {
...
}
bar = () => {}
...
}
What's the difference (if any) between the 2 below ways of defining a method inside a React component?
export default class Foo extends React.Component<
bar() {
...
}
bar = () => {}
...
}
This isn't really a React-specific question and more about classes in JavaScript.
The first one is a method, the second is a property to which an arrow function is assigned. The practical difference is that the second one will be bound to the object (i.e. this
) and the first one will not.
In React this is typically used if the function will be passed to another function or component as a callback.