I am using a class component with react and this error popped up. Wondering, did anyone use a this
inside axios
before or know how to? Thank you in advance
type State = {
fetchedPassword: string;
fetchedPassword: string;
}
type Props = {
}
export default class MyComponent extends React.Component<Props,State>() {
constructor(props: Props) {
super(props);
this.state= {
fetchedPassword: "",
fetchedUsername: ""
}
}
authLogin = (e:any) => {
e.preventDefault();
const { fetchedUsername, fetchedPassword } = this.state;
axios
.get(url)
.then(function (response) {
this.setState({ fetchedPassword: response.data.password }); //error appears here
this.setState({ fetchedUsername: response.data.username }); //and here
})
.catch(function (error) {
console.log("Error: " + error);
});
}
}
The error says
'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
MyComponenet.tsx(26, 13): An outer value of 'this' is shadowed by this container.
I'm not sure how to solve this