I try to fetch data from API using a function and then setState in my React provider.
It works fine if I'm doing it in componentDidMount
directly like this:
componentDidMount() {
axios({
method: "post",
url: "https://example.com",
// data: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
}).then(res => {
this.setState({
TabPass: res.data
});
});
}
I tried to get to the same result but with axios put in function in a separate file and once I get the data, I perform a setState in my provider.
I've tried this in api.js
export function GetPass ()
{
axios({
method: "post",
url: https://example.com",
// data: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
})
.then(res => {
return res.data;
}
)
}
In Provider.js
componentDidMount() {
const Tab = GetPass();
this.setState({
TabPass:Tab,
})
}
}
Tab is undefined