MCPcopy Index your code
hub / github.com/Tencent/CodeAnalysis / useFetch

Function useFetch

web/packages/shared/hooks/useFetch.ts:76–130  ·  view source on GitHub ↗
(
  fetchApi: FetchApi<ApiArgs>,
  apiArgs: ApiArgs,
  options: UseFetchOptions = {},
)

Source from the content-addressed store, hash-verified

74 * @returns [state, reload]
75 */
76const useFetch = <ApiArgs extends any[]>(
77 fetchApi: FetchApi<ApiArgs>,
78 apiArgs: ApiArgs,
79 options: UseFetchOptions = {},
80): [State, () => void] => {
81 const { initData = null, onPreHandler, onSuccess, onFail, onFinally } = options;
82 const [state, dispatch] = useReducer(fetchReducer, {
83 isLoading: false,
84 isError: false,
85 error: '',
86 data: initData,
87 });
88
89 // 用于刷新请求
90 const [refresh, setRefresh] = useState<boolean>(false);
91 const reload = () => setRefresh(!refresh);
92
93 useDeepEffect(() => {
94 // 组件销毁时 abort 数据
95 let didCancel = false;
96 const fetchData = async () => {
97 // 当请求中时,避免多次请求
98 if (state.isLoading) return;
99 // 前置处理
100 onPreHandler?.();
101 // fetch init
102 dispatch({ type: 'FETCH_INIT' });
103 try {
104 const res = await fetchApi(...apiArgs);
105 // 当组件销毁时,将isLoading,isError置为false
106 if (didCancel) {
107 dispatch({ type: 'FETCH_CANCEL' });
108 return;
109 }
110 dispatch({ type: 'FETCH_SUCCESS', payload: res });
111 onSuccess?.(res);
112 } catch (error) {
113 if (didCancel) {
114 dispatch({ type: 'FETCH_CANCEL' });
115 return;
116 }
117 dispatch({ type: 'FETCH_FAILURE', payload: error });
118 onFail?.(error);
119 } finally {
120 onFinally?.();
121 }
122 };
123 fetchData();
124 return () => {
125 didCancel = true;
126 };
127 }, [apiArgs, refresh]);
128
129 return [state, reload];
130};
131
132export default useFetch;

Callers 10

UsersFunction · 0.90
ToolsFunction · 0.90
JobsFunction · 0.90
OrgsFunction · 0.90
ProjectTeamsFunction · 0.90
NodeTableFunction · 0.90
ReposFunction · 0.90
LoadInitServiceFunction · 0.90
ToolLibsFunction · 0.90
NodeTableFunction · 0.90

Calls 2

useDeepEffectFunction · 0.85
fetchDataFunction · 0.85

Tested by

no test coverage detected