Completed
Push — master ( ea5ec6...9a20b4 )
by Alejandro
13s queued 11s
created

src/utils/helpers/hooks.js

Complexity

Total Complexity 0
Complexity/F 0

Size

Lines of Code 20
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 0
eloc 16
mnd 0
bc 0
fnc 0
dl 0
loc 20
ccs 5
cts 10
cp 0.5
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { useState } from 'react';
2
3 2
const DEFAULT_TIMEOUT_DELAY = 2000;
4
5 2
export const useStateFlagTimeout = (setTimeout) => (initialValue = true, delay = DEFAULT_TIMEOUT_DELAY) => {
6
  const [ flag, setFlag ] = useState(initialValue);
7
  const callback = () => {
8
    setFlag(!initialValue);
9
    setTimeout(() => setFlag(initialValue), delay);
10
  };
11
12
  return [ flag, callback ];
13
};
14
15 2
export const useToggle = (initialValue = false) => {
16 2
  const [ flag, setFlag ] = useState(initialValue);
17
18 2
  return [ flag, () => setFlag(!flag) ];
19
};
20