const [ user, setUser ] = useState(JSON.parse(localStorage.getItem('profile'))); //convert to object
const logout = () =>{
dispatch({type: 'LOGOUT'});
history.push("/");
setUser(null);
}
useEffect(()=>{
const token = user?.token;
//JWT check if token expired
if(token){
const decodedToken = decode(token)
if(decodedToken.exp*1000 < newDate().getTime()) logout();
}
setUser(JSON.parse(localStorage.getItem('profile')))
},[location])
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter