This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
react-rest-request/target/request_hook.js

16 lines
698 B
JavaScript
Raw Normal View History

2021-06-15 00:11:58 +03:00
import React from 'react';
import invariant from 'tiny-invariant';
2021-06-23 02:30:19 +03:00
import { methodWithoutEffects } from './endpoint';
2021-06-15 00:11:58 +03:00
import { useLazyRequest } from './lazy_request_hook';
export function useRequest(endpoint, config) {
2021-06-23 02:30:19 +03:00
invariant(methodWithoutEffects(endpoint.method), `You cannot use useRequest with ${endpoint.method} method`);
2021-06-15 00:11:58 +03:00
const [handler, state] = useLazyRequest(endpoint, config);
const skip = React.useMemo(() => { var _a; return (_a = config === null || config === void 0 ? void 0 : config.skip) !== null && _a !== void 0 ? _a : false; }, [config]);
React.useEffect(() => {
if (!skip) {
2021-06-23 02:30:19 +03:00
handler();
2021-06-15 00:11:58 +03:00
}
}, [skip, handler]);
return state;
}