Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

zustand stores manage loading state

const createFetchStore = (fetchActionName, baseUrl) => create(set => {
  result: null,
  loading: false,
  error: null,
  [fetchActionName]: async (id)=> {
    try {
      set({ loading: true })
      const result = await (await fetch(`${baseUrl}/${id}`)).json()
      set({ loading: false, result })
    } catch(error) {
      set({ loading: false, error })
    }
  },
});
Source by github.com #
 
PREVIOUS NEXT
Tagged: #zustand #stores #manage #loading #state
ADD COMMENT
Topic
Name
1+5 =