I'm using React and inside my componentDidMount() {}
I'm calling an imported function and want to access this.setState
within it.
Within componentDidMount
, When I console.log(this)
, I get the component as expected.
But if I run myFunction.call(this)
, console.log(this)
inside myFunction returns undefined
.
Any ideas why this
is not being passed correctly?
edit1: adding code.
Component:
class Navbar extends React.Component {
componentDidMount() {
myFunction.call();
}
}
myFunction.js:
export const myFunction = () => {
console.log('this:', this);
}
Console output:
this: undefined
edit2: some people flagged this as a duplicate of "can i bind arrow functions" but myFunction is an arrow function and even if I call it directly instead of using .call() it produces the same result: this
is undefined.