按钮权限控制组件

63 阅读1分钟
const getUserRole = () => 'admin'

const Authority = ({ value, children }) => {
    const [hasAuthority, setHasAuthority] = useState<boolean>(false)

    useEffect(() => {
        const checkAuthority = async() => {
            const userRole = await getUserRole()
            setHasAuthority(userRole === value)
        }
        checkAuthority()
    }, [value])

    if (hasAuthority) {
        return <div>{children}</div>
    } else {
        return null
    }
}