Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import { useCallback, useEffect } from 'react';
  2. import merge from 'lodash/merge';
  3.  
  4. import { useRequest } from './useRequest';
  5. import { useUpdatingRef } from './useUpdatingRef';
  6.  
  7. export function useFetchRequest({ lazy = false, ...options } = { lazy: true }) {
  8. const optionsRef = useUpdatingRef(options);
  9.  
  10. const [requestState, request] = useRequest(lazy ? {} : { isLoading: true });
  11.  
  12. const refetch = useCallback(
  13. function refetch(optionsUpdate = {}) {
  14. request(merge(optionsRef.current, optionsUpdate));
  15. },
  16. [request, optionsRef],
  17. );
  18.  
  19. useEffect(() => {
  20. if (!lazy) {
  21. refetch();
  22. }
  23. }, []); // eslint-disable-line react-hooks/exhaustive-deps
  24.  
  25. return [requestState, refetch];
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement