简体   繁体   English

React Native - useRef 在 RNCamera 上给出空值

[英]React Native - useRef is giving null value on RNCamera

I am using react-native-camera for the first time and I am using functional component for that.我第一次使用 react-native-camera 并且为此使用了功能组件。 I want to open the camera on button click but using the ref is not working.我想在单击按钮时打开相机,但使用 ref 不起作用。 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 ?我使用useEffect记录了cameraRefcameraRef.current仍然为空,我不明白如何打开相机?

in docs of RNCamera, it should be like this在 RNCamera 的文档中,应该是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM