![](/img/trans.png)
[英]how to call a function that is inside another function in react native
[英]Call a function from inside of another function in React Native
我想从 HelloWorldApp function 内部调用 snapShotTaker function 但我收到ERROR TypeError: undefined is not a function, js engine: hermes
in terminal。 如何解决这个问题呢?
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);
};
您根本无法在组件外部调用它。 但是,您可以使用useEffect 挂钩将通知代码移动到组件内部并在那里调用它。
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.
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.