【发布时间】:2022-10-13 17:11:27
【问题描述】:
问题很清楚,如标题所示,我想知道这两个不同的功能实现与useEffect 之间有什么具体区别吗?
这是一个例子:
const lookAtCameraPermission = async (status: CameraPermissionStatus): Promise<void> => {
switch (status) {
case 'authorized':
setIsCameraAllowed(true);
console.log('Camera permission granted.');
break;
case 'not-determined':
case 'denied':
setIsCameraAllowed(false);
console.log('Camera permission denied or not determined');
await requestCameraPermission();
break;
default:
setIsCameraAllowed(false);
throw new Error('Unknown camera permission status.');
}
};
useEffect(() => {
Camera.getCameraPermissionStatus().then(lookAtCameraPermission).catch(console.log);
}, [lookAtCameraPermission]);
当试图实现这样的事情时,ESLint 说:
The 'lookAtCameraPermission' function makes the dependencies of useEffect Hook (at line 66) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'lookAtCameraPermission' in its own useCallback() Hook.eslintreact-hooks/exhaustive-deps
我更喜欢使用useCallback,但我不能确定这是实现它的最佳方式。
【问题讨论】:
标签: javascript reactjs typescript react-native react-hooks