0

我有以下问题。我应该如何向使用 zustand 存储我的数据的用户显示错误?我有一个showError通过我的反应应用程序使用的功能来显示错误。这个想法是我传递一条错误消息并向用户显示敬酒。

ItemsStore.ts

        try {
            const currentItem = await getItem(itemId);
            set(state => {
               state.items = [...state.items, currentItem]
            });
        } catch (error){
            // Example error: Item doesn't exist.
            // How to show my error to the user
            // I can't use my showError function here,
            // it should be inside a component to not
            // break the rules of hooks
        }
const MyComponent = () => {
   const items = useItemStore(state=>state.items);
   // I don't have an access what happens intern in my store
   // If error occurs the items are an empty array.
}
4

0 回答 0