0

我有以下代码,

import create from "zustand";
import { useAuth } from "react-oidc-context";
import UserService from "../../services/authentication/userService";

type UserStore = {
    role: string,
    setRole: () => void
}

const auth = useAuth();

const useUserStore = create<UserStore>((set, get) => ({
    role: getRole, //how to return getRole value?
    setRole: () => {
        UserService.getUserRole(auth.user.access_token).then((res) =>
            set((state) => ({
                ...state,
                role: res
            }))
        )
    },
    getRole: () => {
        const role = get().role;
        if (!role) {
            get().setRole();
        }

        return role;
    }
}));

export default useUserStore

我希望role价值来自getRole功能,我该怎么做?

4

0 回答 0