Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

zustand stores manage loading state

// This is the store for Page1
const usePageModel1 = create(set => {
  result: null,
  loading: false,
  error: null,
  fetchData1: async (id)=> {
    try {
      set({ loading: true })
      const result = await (await fetch(`/api1/${id}`)).json()  // load data from api1
      set({ loading: false, result })
    } catch(error) {
      set({ loading: false, error })
    }
  },
});

const Page1 = () => {
   const model = usePageModel1();
   // ...
}

// This is the store for Page2
const usePageModel2 = create(set => {
  result: null,
  loading: false,
  error: null,
  fetchData2: async (id)=> {
    try {
      set({ loading: true })
      const result = await (await fetch(`/api2/${id}`)).json()  // load data from api2
      set({ loading: false, result })
    } catch(error) {
      set({ loading: false, error })
    }
  },
});

const Page2 = () => {
   const model = usePageModel2();
   // ...
}
Comment

zustand stores manage loading state

// This is the store for Page1
const usePageModel1 = create(set => {
  result: null,
  loading: false,
  error: null,
  fetchData1: async (id)=> {
    try {
      set({ loading: true })
      const result = await (await fetch(`/api1/${id}`)).json()  // load data from api1
      set({ loading: false, result })
    } catch(error) {
      set({ loading: false, error })
    }
  },
});

const Page1 = () => {
   const model = usePageModel1();
   // ...
}
Comment

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 })
    }
  },
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery keypress div color change 
Javascript :: rotate image javascript base64 
Javascript :: Compare a Boolean with another value 
Javascript :: how to create a new window with a specifc link jquery 
Javascript :: Change tilte alert 
Javascript :: how to find the GCD in javascript 
Javascript :: devlop 
Javascript :: ajax each 
Javascript :: node js rest with flutter 
Javascript :: mongoose auto delete after time 
Javascript :: Expo Location Error: Unhandled promise rejection: Error: Not authorized to use background location services 
Javascript :: React Router rendering blank pages for all components 
Javascript :: js remove child with index 
Javascript :: AngularJS Graphs & Charts - Mix of solid & dotted 
Javascript :: ! function in javascript 
Javascript :: AngularJs - Display Validation Message within Component 
Javascript :: Why am I getting an error, "ReferenceError: categories is not defined" in AngularJS 
Javascript :: react-native navigation stack set push component then cover parent page 
Javascript :: How to hover over data inserted from JSON 
Javascript :: get lat long from address google api 
Javascript :: socket.io authentication 
Javascript :: insert image in jsp page 
Javascript :: Simple Mustache.js 
Javascript :: javascript assignment by reference or value 
Javascript :: Bare Minimum Passport Login Pages App.js 
Javascript :: Make Floating label TextInput with password show/hide in react native 
Javascript :: vue js beforeEach is not a function 
Javascript :: Get First Second Third Term In Array 
Javascript :: Inside Fetch Is A Request 
Javascript :: json to dart dummy api 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =