0

I am using react-native-camera for the first time and I am using functional component for that. I want to open the camera on button click but using the ref is not working. Here is my code :

const camContainer = () => {
  const cameraRef = useRef(null);

  useEffect(() => {
    console.log(cameraRef);
  }, [cameraRef]);

  const openCamera = () => {
    const options = {quality: 0.5, base64: true};
    // cameraRef.current.takePictureAsync(options);
    console.log(cameraRef);
  };

  return (
    <TouchableOpacity onPress={() => openCamera()}>
      <Text>Open Camera</Text>
      {!cameraRef && (
        <View>
          <RNCamera ref={cameraRef} style={{flex: 1, alignItems: 'center'}} />
        </View>
      )}
    </TouchableOpacity>
  );
};

I logged cameraRef using useEffect but the cameraRef.current was still null, I cannot understand how do I open the camera then ?

matthias_h
  • 11,356
  • 9
  • 22
  • 40
suvodipMondal
  • 656
  • 10
  • 27

1 Answers1

-2

in docs of RNCamera, it should be like this

<Camera
  ref={ref => {
    this.camera = ref;
  }}
/>;
// ...
snap = async () => {
  if (this.camera) {
    let photo = await this.camera.takePictureAsync();
  }
};
M. Koch
  • 27
  • 3