I took a two day React training class last week. The instructor tried to jam a weeks worth of info into two days. We slapped together a crude blackjack game in the "class" (or is that Class<T>: => () await (...p) ha ha little Typescript joke there.) The class did not get around to covering how to access a REST API. So here I am stuck with a bunch of confusing unreadable code that I am not even sure is React or Typescript. Any whooo this is my App.tsx file:
import React, {useState} from 'react';
import './App.css';
import {fetchUser, User, UserCtx} from "./User";
import {useAsync} from "./ss-react-utils";
const AppLoaded = ({user}: {user: User}) => {
return <div className="App">
{user.userName}
</div>;
};
const AppLoading = () => {
return <div>Loading ...</div>;
};
const App: React.FC = () => { // I guess the FC stands for F*king Confusing
const [user, setUser] = useState<User | null>(null);
useAsync({
op: fetchUser,
onSuccess: setUser,
deps: []
});
if (user !== null) {
return <AppLoaded user={user} />;
} else {
return <AppLoading/>;
}
}
export default App;
Here's the ss-react-utils.tsx file:
import React, {CSSProperties, DependencyList, useEffect} from "react";
export type Op<T> = () => Promise<T>;
export type OnSuccess<T> = (data: T) => void;
export function useAsync<T>({op, onSuccess, deps}: { op: Op<T>, onSuccess: OnSuccess<T>, deps?: DependencyList }) {
useEffect(() => {
const doOp = async () => {
const data: T = await op();
onSuccess(data);
};
doOp();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}
... some other unrelated ( I think ) code ...
when I run that code I get this ...

I would like to know how to handle the error and display the login page to the user if the REST API returns a 403 forbidden error.
And Yes I know the REST API is returning a 403 error. So please don't answer the question with you are getting a 403 error. :)
Also I have seen a lot of answers that say use componentDidMount but the instructor said that was the old way of doing React and he was going to teach about hooks instead. Well would like to try what the instructor was getting at before I completely give up on the "New" way to code React. And probably React all together. So please help :)
UPDATE: I am using Django Rest Framework and it is behaving as expected.

Promise.rejectthat error and in the catch set error. You currently are not setting a loading state before the async and do not check if the component is still mounted when the promise resolves and you set state you cannot set state on unmounted component so you should check if it's still mounted after an async process finishes and before setting state.