SHARE
TWEET

Untitled

a guest Jul 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function useAsyncState<T>(initialState: T): [T, (arg0: (arg0: T) => T | T) => Promise<T>] {
  2.     const [state, setState] = React.useState(initialState);
  3.  
  4.     const setAsyncState = React.useCallback((stateUpdate: (arg0: T) => T | T): Promise<T> => {
  5.         return new Promise<T>((resolve) => {
  6.             setState((prevState: T) => {
  7.                 let newState = null;
  8.                 if (typeof stateUpdate === "function") {
  9.                     newState = stateUpdate(prevState);
  10.                 } else {
  11.                     newState = stateUpdate;
  12.                 }
  13.                 resolve(newState);
  14.                 return newState;
  15.             });
  16.         });
  17.     }, []);
  18.     return [state, setAsyncState];
  19. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top