[英]React Native Hook in Class converting function to class variable undefined
We are attempting to convert a react native function into a class so we can include state management that will work with Firebase real time database.我们正在尝试将 react native 函数转换为一个类,以便我们可以包含将与 Firebase 实时数据库一起使用的状态管理。 We just realized you cannot use a Hook in a class so would appreciate some guidance as to the alternative that suites the situation the best.我们刚刚意识到你不能在课堂上使用 Hook,所以希望得到一些关于最适合情况的替代方案的指导。 The app is a simple camera that allows you to record and post videos.该应用程序是一个简单的相机,可让您录制和发布视频。
Here is the original code (works) snippet in a function.这是函数中的原始代码(作品)片段。 const type is decalred and uses hook useEffect. const 类型被贴标并使用钩子 useEffect。 We need an alternative that works in a class.我们需要一个在类中起作用的替代方法。
export function App({ navigation }) {
const [hasPermission, setHasPermission] = useState(null);
const [cameraRef, setCameraRef] = useState(null)
const [recording, setRecording] = useState(false)
//type uses hooks declared HERE
const [type, setType] = useState(Camera.Constants.Type.back); useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
Here is what we're working on:以下是我们正在开展的工作:
export class App extends Component {
state = {
hasPermission: null,
cameraRef: null,
recording: false,
type: Camera.Constants.Type.back //can not find variable type HERE
};
componentDidMount() {
async () => {
status = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
}, []; if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
}
Any help you be appreciated, thank you.任何帮助您不胜感激,谢谢。
try this尝试这个
export class App extends Component {
state = {
hasPermission: null,
cameraRef: null,
recording: false,
type: Camera.Constants.Type.back //can not find variable type HERE
};
async componentDidMount() {
status = await Camera.requestPermissionsAsync();
this.setState({
hasPermission: 'granted'
});
if (this.state.hasPermission === null) {
return <View />;
}
if (this.state.hasPermission === false) {
return <Text>No access to camera</Text>;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.