javascript - 如何在 onpress 处于活动状态时将位置数据存储到 Asyncstorage
问题描述
所以基本上我想在按下按钮处于活动状态时将位置数据存储到异步存储中。我已经阅读了 asyncstorage 文档,但是我仍然卡住并且仍然需要一些提示和更多示例来进行编码。
有人能帮我吗?
export default class index extends Component {
findCoordinates = () => {
Geolocation.getCurrentPosition(
(position) => {
const location = JSON.stringify(position);
this.setState({ location });
},
(error) => Alert.alert(error.message),
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000,
forceRequestLocation: true,
}
);
};
render() {
return (
<Container>
<ScrollView>
<View style={styles.top}></View>
<Card
containerStyle={{
borderRadius: 10,
marginTop: -30,
}}
>
<View>
<TouchableOpacity onPress={this.findCoordinates}>
<Text>Find My Coords?</Text>
<Text>Location: {this.state.location}</Text>
</TouchableOpacity>
</View>
</Card>
</ScrollView>
</Container>
);
}
}
解决方案
假设您已经导入了以下内容:
import {AsyncStorage} from 'react-native';
点击按钮后请添加以下代码:
AsyncStorage.setItem('storedlocation', this.state.location);
下一次,如果你想获取 asyncstorage 的值,说进入“retrievedlocation”的状态,请使用以下代码:
getstoredlocationFunction = () => {
AsyncStorage.getItem('storedlocation').then(
value2 =>
this.setState({ retrievedlocation: value2 })
);
};
祝你今天过得愉快。
推荐阅读
- python - 如何使用solve_ivp求解复杂的矩阵微分方程?
- apache - 使用 GET 参数将所有子文件夹重定向到 URL
- c# - MVC FileStreamResult 未下载具有自定义文件名的文件
- ruby-on-rails - 锚标记未路由到 Rails 中的预期页面
- ios - 将 IndexPath 插入 NSSet 目标-C
- ios - xam.plugin.geolocator 在 ios 中不起作用
- memory-leaks - 如何检测 C++ Builder 10.4 / Android 应用程序中的泄漏
- javascript - 用 Puppeteer 覆盖 showOpenFilePicker
- sql - 仅从“已添加”/“已删除”列中查找活动项目
- c# - 使用 .BeginInvoke 启动的委托不断抛出 ThreadAbortExceptions?