I have a strange problem that keeps me scratching my head. I'm trying to get data from a api and pass the returned data to a component (as a prop) with React Router. However the returned data is 'undefined'.
MyRoutes.js:
let refreshAvailability = (profile_index) => {
let fetch = new DataFetch();
fetch.get(`/info/${profile_index}`, (err, res) => {
let username = res[Object.keys(res)[0]].username;
if(username !== undefined) {
setTimeout(() => {return getStatus(username)}, 0);
setInterval(() => {return getStatus(username)}, 10000); //Refresh every 10 seconds
}
});
}
let getStatus = (username) => {
let userStatus = new userStatus();
userStatus.getStatus(available => {
//If i place console.log(available) here it will print the result, but when i return it, its undefined.
return available;
});
}
const myRoutes = () => (
<BrowserRouter>
<Route exact path='status/:profile_index' render={(props) => (
<Status {...props} available={refreshAvailability} />
)}/>
</BrowserRouter>
);
export default MyRoutes;
Status.js:
componentDidMount(){
let userIndex = this.getUserIndex();
let userStatus = this.props.available(userIndex);
console.log(userStatus); //This prints undefined
}
Somehow the problem seems to be in the getStatus function. If i log the output of the api there i can see it logging in the Status component, but when i return it it returns 'undefined'.
This leaves me to the question how React is handling functions with a return statement as a prop? Is it any different than passing a function without return? Or am i overlooking things in my code?