SHARE
TWEET

Untitled

a guest Apr 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2.  
  3. /**
  4.  * extend the React.useState to have the state referente, so it will be possible to use its value
  5.  * inside of other callbacks
  6.  *
  7.  * @param {T} initialValue
  8.  *
  9.  * @return {[T, React.MutableRefObject<T>, React.Dispatch<React.SetStateAction<T>>]} array
  10.  */
  11. export function useRefState<T>(
  12.     initialValue: T
  13. ): [T, React.MutableRefObject<T>, React.Dispatch<React.SetStateAction<T>>] {
  14.     const [state, setState] = React.useState<T>(initialValue);
  15.     const stateRef = React.useRef<T>(state);
  16.     React.useEffect(() => {
  17.         stateRef.current = state;
  18.     }, [state]);
  19.     return [state, stateRef, setState];
  20. }
  21.  
  22. export default useRefState;
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