2

I want to call snapShotTaker function from inside of HelloWorldApp function but i get ERROR TypeError: undefined is not a function, js engine: hermes in terminal. How to solve this problem?

notifee.registerForegroundService(notification => {
  return new Promise(() => {
    // Long running task...
    setInterval(() => {
      HelloWorldApp.snapShotTaker();
    }, 2000);
  });
});

const HelloWorldApp = () => {
  const isAppForeground = useIsForeground();
  console.log('In Foreground?: ', isAppForeground);

  const cameraRef = useRef(null);
  const [finalPath, setPhotoPath] = useState('');

  const devices = useCameraDevices();
  const device = devices.front;

  if (device == null) {
    return <ActivityIndicator style={styles.indicator} size="large" />;
  }

  const snapShotTaker = async () => {
    const snapshot = await cameraRef.current.takeSnapshot({
      quality: 20,
      skipMetadata: true,
    });
    setPhotoPath('file://' + snapshot.path);
  };
2
  • try to create you snapShotTaker function using function keyword Commented Jan 5, 2023 at 6:06
  • @TalhaJutt i tried but i got same error. Commented Jan 5, 2023 at 6:14

1 Answer 1

3

You simply can't call it outside of the component. However you can move the notifee code to inside of your component using the useEffect hook and call it there.

const HelloWorldApp = () => {
  const isAppForeground = useIsForeground();
  console.log('In Foreground?: ', isAppForeground);

  const cameraRef = useRef(null);
  const [finalPath, setPhotoPath] = useState('');

  const devices = useCameraDevices();
  const device = devices.front;

  if (device == null) {
    return <ActivityIndicator style={styles.indicator} size="large" />;
  }

  const snapShotTaker = async () => {
    const snapshot = await cameraRef.current.takeSnapshot({
      quality: 20,
      skipMetadata: true,
    });
    setPhotoPath('file://' + snapshot.path);
  };

  useEffect(() => {
    // Register your service
    notifee.registerForegroundService(notification => {
      return new Promise(() => {
        // Long running task...
        setInterval(() => {
         snapShotTaker();
        }, 2000);
      });
    });

    // You need to stop the service on cleanup, so it doesn't register multiple times.
    // See https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup
    return () => {
      notifee.stopForegroundService();
    };
  
  // You can pass the snapShotTaker function to the deps array but it won't affect it since it isn't memoized. See useCallback hook if you want to memoize it.
  });
Sign up to request clarification or add additional context in comments.

8 Comments

I recommend adding an empty dependency array to the useEffect unless you want to register for the service every time something changes in the component.
I didn't want to put it in my answer since i don't know how he wants to use it, so added a comment in the closing line. But yeah, an empty array could be used if OP doesn't use (doesn't use in the question but, you know, might) variables that can change in snapShotTaker function.
@UgurEren your code is working when app is open but when i close the app i get this error: session/camera-not-ready: [session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!
You need to wait for the camera to initialize as the error states. According to vision camera docs, you can pass a function to the onInitialized prop and start taking snapshots when that function is called. You can do this by storing a boolean state with false default value, setting this state's value to true on the onInitialized props callback function, and checking if this state's value is true in the snapShotTaker function before calling the takeSnapshot method.
@UgurEren I asked a new question for my problem here: stackoverflow.com/questions/75017209/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.