pmndrs / zustand

🐻 Bear necessities for state management in React

Home Page:https://zustand-demo.pmnd.rs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Argument of type '(state: ClientStore) => Client | undefined' is not assignable to parameter of type 'ClientStore | Partial<ClientStore> | ((state: ClientStore) => ClientStore | Partial<ClientStore>)'. Type '(state: ClientStore) => Client | undefined' is not assignable to type '(state: ClientStore) => ClientStore | Partial<ClientStore>'. Type 'Client | undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'. Type 'undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'.ts(2345)

bishalk21 opened this issue · comments

HELP ME:
import create from "zustand";
import { Client } from "./types";

interface ClientStore {
clients: Client[];
addClient: (client: Client) => void;
getClientById: (clientId: string) => Client | null;
}

export const useClientStore = create((set) => ({
clients: [],
addClient: (client) =>
set((state) => ({ clients: [...state.clients, client] })),
getClientById: (clientId) => {
const client = set((state) =>
state.clients.find((c: Client) => c.id === clientId)
);
return client || null;
},
}));