I have the following function in my ReactJS app:
function MyView() {
const [mydata, setMyData] = useState({ mydata: {} });
const [mystatus, setMyStatus] = useState({ mystatus: null });
useEffect(() => {
let config = {
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('my-token'),
'Content-Type': 'application/json'
}
}
const fetchData = async () => {
const result = await axios(
'http://localhost:8000/endpoint/' + some_id + "/",
config
);
setMyStatus(result.status);
setMyData(result.data);
};
fetchData();
}, []);
.... <Loading the rest of functions and HTML here>
I want to make sure that the mystatus is 200 and that mydata.view == public before I load anything here. The problem is that JS loads rest of the page before the response is received from server.
How do I stop it from doing that i.e, redirect to another page if mystatus !== 200 or mydata.view !== 'public'
