TypeError: undefined is not an object (evaluating '_this.camera = _ref')

TypeError: undefined is not an object (evaluating '_this.camera = _ref')

-React Native 中的应用程序开发- 你好, 我对 Expo Camera 有疑问。此处提示拍照时出现错误

“TypeError: undefined 不是一个对象(评估'_this.camera = _ref')”/Scan.js.

如果使用 Expo 对应用程序进行全新更新,则一切正常。但是一旦您继续编程并发生另一个错误,此错误就会出现并且不会消失,直到您再次刷新应用程序。

我已经尝试了很多,但我在这里需要帮助。

Scan.js

import React, { Component, useState, useEffect } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Image } from 'react-native';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
import {Camera, Constants} from 'expo-camera';
import * as MediaLibrary from 'expo-media-library';
import * as Haptics from 'expo-haptics';


import Images from '../assets/icon/index'



const Scan = () => {
  const [hasPermission, setHasPermission] = useState(null);
  const [type, setType] = useState(Camera.Constants.Type.back);
  const [status, requestPermission] = MediaLibrary.usePermissions();
  


  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>;
  }
  
  takePicture = async () => {
    if (this.camera) {
      let photo = await this.camera.takePictureAsync();

      Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);

      console.log(photo.uri);
      MediaLibrary.saveToLibraryAsync(photo.uri);
    }
  };

  
  
  
  

  
  return (
    <View style={styles.container}>
      <Camera style={styles.camera}
        type={type}
        ref={ref => {
          this.camera = ref;
        }}>
        <View style={styles.buttonContainer}>
          <TouchableOpacity
            style={styles.button}
            onPress={() => {
              setType(
                type === Camera.Constants.Type.back
                  ? Camera.Constants.Type.front
                  : Camera.Constants.Type.back
              );
            }}
            >
            <Image source={Images.camera} style={styles.icon}></Image>
          </TouchableOpacity>
          <TouchableOpacity
          style={styles.button}
          onPress={takePicture}
          >
            <Text style={styles.text}>Take</Text>
          </TouchableOpacity>
        </View>
      </Camera>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  camera: {
    flex: 1,
  },
  buttonContainer: {
    flex: 1,
    backgroundColor: 'transparent',
    flexDirection: 'row',
    margin: 20,
    top: 0,
  },
  button: {
    flex: 0.1,
    alignSelf: 'flex-end',
    alignItems: 'center',
  },
  text: {
    fontSize: 18,
    color: 'white',
  },
  icon : {
    tintColor: 'white',
  },
})

export default Scan; ```

创建新的相机参考并将其附加到 Camera 组件。

import { useRef } from 'react';
...
const cameraRef = useRef<Camera>(null);
...
<Camera ref={cameraRef} ... />

在您的 takePicture 函数中将 this.camera.takePictureAsync 替换为 cameraRef.current?.takePictureAsync