tRPCttRPC
Powered by
SonS
tRPC•4y ago•
3 replies
Son

zustand + trpc (basic data fetching)

im trying to use zustand with trpc, but having trouble setting some data to global state. What am i missing here?

import create from 'zustand';
import { trpc } from '../../utils/trpc';

interface TenantState {
  tickets: [];
  setTickets: any;
}

export const useTenantStore = create<TenantState>()((set, get) => ({
  tickets: [],
  setTickets: async (newTicket: any[]) => {
    set(() => ({ tickets: trpc.ticketService.getByTenantId.useQuery().data })); // problem area
  },
}));
import create from 'zustand';
import { trpc } from '../../utils/trpc';

interface TenantState {
  tickets: [];
  setTickets: any;
}

export const useTenantStore = create<TenantState>()((set, get) => ({
  tickets: [],
  setTickets: async (newTicket: any[]) => {
    set(() => ({ tickets: trpc.ticketService.getByTenantId.useQuery().data })); // problem area
  },
}));


i've also tried this with no luck

export const useTenantStore = create<TenantState>()((set, get) => ({
  allTickets: {},
  fetch: () => {
    const data = trpc.ticketService.getByTenantId.useQuery().data;
    set({ allTickets: data });
  },
}));
export const useTenantStore = create<TenantState>()((set, get) => ({
  allTickets: {},
  fetch: () => {
    const data = trpc.ticketService.getByTenantId.useQuery().data;
    set({ allTickets: data });
  },
}));


and this...

  React.useEffect(() => {
    fetch(trpc.ticketService.getByTenantId.useQuery());
  }, []);
  React.useEffect(() => {
    fetch(trpc.ticketService.getByTenantId.useQuery());
  }, []);


  const fetch = useTenantStore((state) => state.fetch);
  const fetch = useTenantStore((state) => state.fetch);


export const useTenantStore = create<TenantState>()((set, get) => ({
  allTickets: {},
  fetch: async (url: any) => {
    const data = url.data;
    set({ allTickets: data });
  },
}));
export const useTenantStore = create<TenantState>()((set, get) => ({
  allTickets: {},
  fetch: async (url: any) => {
    const data = url.data;
    set({ allTickets: data });
  },
}));
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Zustand + trpc
Answer OverflowAAnswer Overflow / ❓-help
10mo ago
Adding tRPC HOC breaks zustand
ThomasTThomas / ❓-help
3y ago
tRPC Query Invalidation Issue with Client-Side Data Fetching and Initial Data from server component
devianaDdeviana / ❓-help
16mo ago