【发布时间】:2020-08-18 03:09:44
【问题描述】:
我需要这个:const setError = useError(); 作为 useEffect 中的依赖项,但由于此函数也用于其他地方(在同一组件内),所以每当抛出错误时,我的 useEffect api 重新获取数据.
我应该禁用react-hooks/exhaustive-deps 规则还是有办法解决这个问题?如果我尝试将它包装在 useCallback 中,我会收到一个错误,即 hooks 只能在组件本身中使用。
编辑
export const useError = (): ((error: any, title?: string) => void) => {
const dispatch = useDispatch();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const setError = (error: any, title = 'Error'): void => {
Sentry.captureException(error);
const bodyText = error.message || error;
const errorTitle = error.name || title;
dispatch(
setNotification({
type: notificationTypes.prompt,
title: errorTitle,
bodyText,
className: 'error',
show: true,
})
);
};
return setError;
};
【问题讨论】:
-
能否展示useError的实现
-
@ShubhamKhatri 编辑了我的问题
标签: reactjs redux jestjs react-hooks enzyme