[英]React native change state from callback
import React, {useState} from 'react';
import BackgroundGeolocation, {
Location,
} from '@mauron85/react-native-background-geolocation';
const startBackgroundService = () => {
BackgroundGeolocation.configure({
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 50,
distanceFilter: 10,
notificationTitle: 'Strings.Geolocation.NOTIFICATION_TITLE',
notificationText: 'Strings.Geolocation.NOTIFICATION_DESCRIPTION',
debug: false,
startOnBoot: false,
stopOnTerminate: true,
locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
interval: 1000,
fastestInterval: 10000,
activitiesInterval: 30000,
stopOnStillActivity: false,
notificationIconColor: '#4CAF50',
notificationIconSmall: 'notification_icon',
});
BackgroundGeolocation.on('authorization', status => {
if (status !== BackgroundGeolocation.AUTHORIZED) {
} else if (status.hasPermissions) {
}
});
BackgroundGeolocation.on('location', location => {
BackgroundGeolocation.startTask(async taskKey => {
console.log(location.longitude);
console.log(location.latitude);
//NEED HERE TO SET VALUE FOR loc from App()
console.log(new Date());
BackgroundGeolocation.endTask(taskKey);
});
});
BackgroundGeolocation.start();
};
startBackgroundService();
export default function App() {
let [loc, setLoc] = useState('t');
//NEED HERE TO DISPLAY loc WHICH I CHANGED FROM CALLBACK BackgroundGeolocation.on
return (
<SafeAreaView style={styles.container}>
<Text>{loc}</Text>
<Button title={'q'} />
</SafeAreaView>
);
}
Hello, I don't know how to change state variable in geolocation callback outside App().您好,我不知道如何在 App() 之外的地理位置回调中更改 state 变量。 I want to display the user's new location on the screen after it changes immediately in callback.
我想在回调中立即更改后在屏幕上显示用户的新位置。 Please, help.
请帮忙。 wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
You cannot access state outside a component, but..... there are three ways to do this (don't remember any more at the moment):您不能在组件之外访问 state,但是.....有三种方法可以做到这一点(暂时不记得了):
You can use the 'location' listener inside a useEffect in your App component like this:您可以在 App 组件的 useEffect 中使用“位置”侦听器,如下所示:
useEffect(() => { BackgroundGeolocation.on('location', location => { BackgroundGeolocation.startTask(async taskKey => { console.log(location.longitude); console.log(location.latitude); //NEED HERE TO SET VALUE FOR loc from App() console.log(new Date()); BackgroundGeolocation.endTask(taskKey); }); }); return () => { /// Here, I'd highly recommend you to unsubscribe to the listener } }, []); useEffect(() => { BackgroundGeolocation.on('location', location => { BackgroundGeolocation.startTask(async taskKey => { console.log(location.longitude); console.log(location.latitude); //这里需要TO SET VALUE FOR loc from App() console.log(new Date()); BackgroundGeolocation.endTask(taskKey); }); }); return () => { /// 在这里,我强烈推荐你取消订阅监听器 } }, []);
You can a custom event emitter to emit updates whenver a location is received - this is helpful when you want to listen to update in several components.您可以使用自定义事件发射器在收到位置时发出更新 - 当您想在多个组件中收听更新时,这很有用。 You can do it like:
你可以这样做:
class LocationHandler extends EventEmitter {} class LocationHandler 扩展 EventEmitter {}
and then in your 'location' callback, you can use it like然后在您的“位置”回调中,您可以像使用它一样
LocationHander.emit('whatever name you want', location-data);
then inside your components, you need to listen to your LocationHandler like in the first step, using a useEffect, but don't forget to remove the event subscription.然后在你的组件中,你需要像第一步一样使用 useEffect 来监听 LocationHandler,但不要忘记删除事件订阅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.