直接上代码
import React,{useEffect, useState} from 'react'
import {useLocation,Navigate} from 'react-router-dom'
export default function IsAuth(props) {
const localocation =useLocation()
const [preTrue,setPretrue]=useState(true)
useEffect(()=>{
if(window.localStorage.getItem('token')){
setPretrue(true)
}else{
setPretrue(false)
}
return ()=>{}
},[localocation.pathname])
return preTrue ? props.Component :<Navigate to='/login'/>
}