React-router-dom6配置路由表的权限

晏晨朗
2023-12-01

直接上代码

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'/>

}

 类似资料: