• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mints-components / hooks / 13319939355

14 Feb 2025 12:36AM CUT coverage: 96.569% (-0.2%) from 96.809%
13319939355

push

github

mintsweet
feat: add a new hook useQueryParams

62 of 68 branches covered (91.18%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 2 files covered. (93.75%)

135 of 136 relevant lines covered (99.26%)

13.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/use-request.ts
1
import { useState, useRef, useEffect, useCallback } from 'react';
16✔
2

3
export const useRequest = <T>(
16✔
4
  request: (signal: AbortSignal) => Promise<T>,
5
  deps: React.DependencyList = [],
8✔
6
) => {
7
  const [state, setState] = useState<{
38✔
8
    loading: boolean;
9
    data?: T;
10
    error?: unknown;
11
  }>({
12
    loading: false,
13
  });
14

15
  const abortControllerRef = useRef<AbortController | null>(null);
38✔
16

17
  const fetchData = useCallback(() => {
38✔
18
    abortControllerRef.current?.abort();
14✔
19
    const abortController = new AbortController();
14✔
20
    abortControllerRef.current = abortController;
14✔
21

22
    setState({ loading: true });
14✔
23

24
    request(abortController.signal)
14✔
25
      .then((data) => {
26
        if (!abortController.signal.aborted) {
12✔
27
          setState({ loading: false, data });
8✔
28
        }
29
      })
30
      .catch((err) => {
31
        if (!abortController.signal.aborted) {
2✔
32
          setState({ loading: false, error: err });
2✔
33
        }
34
      });
35

36
    return () => {
14✔
37
      abortController.abort();
14✔
38
    };
39
  }, deps);
40

41
  useEffect(() => {
38✔
42
    const cleanup = fetchData();
14✔
43

44
    return () => {
14✔
45
      cleanup();
14✔
46
    };
47
  }, [fetchData]);
48

49
  return state;
38✔
50
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc