I am trying to call the navigation function to navigate the user from screen A to screen B if API returns the error 404
. If the user has not created the profile then API will return the error 404
status code and I will catch it in the catch block then I will check if the status code is 404
if yes then I will navigate the user to create profile screen. So I am getting 404
and console logs are getting printed in the terminal but at first render, it’s not navigating the user or the function is not being called.
useEffect Code:
useEffect(() => {
const isUserCreatedProfile = async (loggedInUserInfo) => {
try {
await axios
.get(`${baseUrl}/api/user/profile?email=${loggedInUserInfo?.email}`, {
headers: apiHeaders,
})
.then((res) => {
// Some logic
})
.catch((err) => {
componentErrorHandler("home", "isUserCreatedProfile[axios]", err);
if (err?.response?.status === 404) {
console.log("err got", err?.response?.status);
navigateToCreateProfile(
loggedInUserInfo["custom:Name"],
loggedInUserInfo?.email,
);
console.log("err got here");
}
console.log("err got here 2");
setLoading(false);
});
} catch (err) {
componentErrorHandler("home", "isUserCreatedProfile", err);
}
};
if (userInfo?.data) {
isUserCreatedProfile(userInfo?.data);
}
}, [
chatData?.connected,
connectUserChat,
navigateToCreateProfile,
navigation,
setUserInfo,
token?.accessToken,
userInfo,
userLocationSave,
]);
Navigation Function code:
const navigateToCreateProfile = useCallback(
(name, email) => {
console.log("called navigation");
navigation.replace("CreateProfile", {
name,
email,
});
},
[navigation],
);
Terminal Logs:
LOG home isUserCreatedProfile[axios] [AxiosError: Request failed with status code 404]
LOG err got 404
LOG called navigation
LOG err got here
LOG err got here 2
I have also tried to remove the function navigateToCreateProfile
and add its code inside the catch block only but it still has not worked.
It only works if I refresh the app again then the navigation function gets called.
I am unable to figure out the issue. How can I resolve it?