PeformP
tRPC12mo ago
Peform

trpc useError hook?

Hey, I have a hook which i use for fetching some data using trpc and I'm wondering if there is some sort of useError so i can catch all of the errors for each query, in one variable?
or is the best way using the error property on each of the useQuery invocations?

Currently, if I want to display an error I'd have to make 3 separate variables and parse each of those in the if statement if error at the bottom.
export const GuildProvider = ({ guildId, children, fetchChannels, fetchRoles }: Props) => {
    const isFetching = useIsFetching();
    const { data: guild } = api.guild.get.useQuery({
        guildId,
    });
    const { data: channels } = api.guild.channels.useQuery(
        {
            guildId,
        },
        { enabled: fetchChannels },
    );
    const { data: roles } = api.guild.get.useQuery(
        {
            guildId,
        },
        { enabled: fetchRoles },
    );

    if (isFetching) {
        return <CustomLoader />;
    }

    if (error) {
        return <div>Error fetching guild data: {error.message}</div>;
    }
Was this page helpful?