无效的挂钩调用。钩子只能在函数组件的主体内部调用,错误仅发生在浏览器中而不是移动设备上
无效的挂钩调用。钩子只能在函数组件的主体内部调用。发生这种情况的原因之一可能是:
- 您的 React 和渲染器版本可能不匹配(例如 React DOM)
- 您可能违反了 Hooks 规则
- 中拥有多个 React 副本
您可能在同一个应用程序 当我尝试使用 Expo CLI 提供的相机 API 拍照时出现此错误。我的实际问题是,它在我的 Android 设备上运行没有任何错误,但是每当我在浏览器(即 React DOM)上运行该应用程序时,它都会在控制台中产生此错误。
这是我从 expo doc 复制的代码,
import { Camera } from "expo-camera";
import { useEffect, useState } from "react";
const TakeImage = () => {
const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<Camera style={styles.camera} type={type}>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}
>
<Text style={styles.text}> Flip </Text>
</TouchableOpacity>
</View>
</Camera>
</View>
);
};
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
I'm getting this error when I tried using Camera API provided by Expo CLI to take picture. What my actual issue is that, it runs on my android device without any error, But whenever I run the app on browser i.e., react DOM, it produces this error in console.
Here is the code which I have copied from expo doc,
import { Camera } from "expo-camera";
import { useEffect, useState } from "react";
const TakeImage = () => {
const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<Camera style={styles.camera} type={type}>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}
>
<Text style={styles.text}> Flip </Text>
</TouchableOpacity>
</View>
</Camera>
</View>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能在另一个钩子中使用一个钩子,因为它违反了从 React 函数组件调用 Hooks 的规则,并且你传递给 useEffect 的函数是一个常规的 javascript 函数。您可以做的是在另一个自定义挂钩中调用一个挂钩,如下所示:
您的代码:
更改为:
You can't use a hook inside another hook because it breaks the rule Call Hooks from React function components and the function you pass to useEffect is a regular javascript function. What you can do is call a hook inside another custom hook shown below:
Your code:
Change To: