zidniryi

useToggle.js

Oct 20th, 2021
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useCallback, useState } from 'react';
  2. // Hook
  3. // Default parameters adalah boolean
  4. export const useToggle = (initialState = false) => {
  5.     // Initialize state
  6.     const [state, setState] = useState(initialState);
  7.    
  8.     // Define and memorize untuk toggle funcion
  9.     // Function berubah kebalikan dari state sebelumnya
  10.     const toggle = useCallback(() => setState(state => !state), []);
  11.    
  12.     return [state, toggle]
  13. }
RAW Paste Data