i am trying to create a token, when its expired ... redirect it to login page. but as soon as i add navigate('/login') i am getting an error Error-"React Hook "useNavigate" is called in function"
what am i doing wrong? how can i navigate to login page?
const me = async (token) => {
const navigate = useNavigate()
let config = {
headers: {
Authorization: "Bearer " + token
}};
try {
const response = await axios.get(API_URL + 'api/users/me', config)
} catch (error) {
logout()
reset()
// navigate('/login')
}
}
So useNavigate is a React Hook. As such, it cannot be called within a nested function.
You'll see an example of how to call useNavigate inside the top-level React function in that sandbox.
You may want to check out https://reactjs.org/docs/hooks-rules.html for more references.