I have a single-page app where I build multiples Custom React Hook to fetch data in my backend (MongoDB). Every time, I'm also verifying if the user is logged before fetching data like so:
export const useTotalSessionsToday = () => {
const { user } = useAuth0();
let userName;
if (user) {
userName = user.name;
}
const { data, loading, error } = useQuery(GET_TOTAL_SESSIONS_TODAY, {
variables: { userName },
});
return { data, loading, error };
};
Currently, I'm calling the auth0 hook in every Custom Hook. I find it hard to believe there isn't a better way (since calling auth0 hook is somehow slow).
Should I use localStorage and store the user "username" on page load and then fetch it from there instead? Or is it still better to call auth0 every time?
PS: I use Typescript with React and Graphql