- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已在我的 react native 项目的一个类中保存了一项数据。保存数据后,我开始一个新屏幕。在该屏幕中,我正在检索已保存的数据。为此,我在渲染函数内调用 AsyncStorage.getItem('token') 函数。
这是该类的代码-
import React from 'react';
import { StyleSheet, Text, View, Image, AsyncStorage } from 'react-native';
import {Icon, Button, Container, Header, Content, Left} from 'native-base';
import CustomHeader from './CustomHeader';
class NoteMeHome extends React.Component {
state = {
text:'',
storedValue:'',
getValue: ''
};
static navigationOptions = ({navigation}) => ({
title: "Home",
headerLeft: <Icon name="ios-menu" style={{paddingLeft:10}}
onPress={()=>navigation.navigate('DrawerOpen')}/>,
drawerIcon:
<Image source={require('../assets/icon.png')}
style={styles.icon}
/>
})
render() {
const {storedValue} = this.state;
AsyncStorage.getItem('token').then(value =>
//AsyncStorage returns a promise so adding a callback to get the value
this.setState({ getValue: value })
//Setting the value in Text
);
return(
<Container>
<CustomHeader
title="Home"
drawerOpen={()=>this.props.navigation.navigate('DrawerOpen')}
/>
<Content contentContainerStyle={{flex:1, alignItems:'center',
justifyContent:'center', padding:10}}>
<Button full onPress={()=> this.props.navigation.navigate('Settings')}>
<Text style={{color:'white'}}>{storedValue}</Text>
</Button>
<Text>{this.state.getValue}</Text>
</Content>
</Container>
)
}
}
const styles = StyleSheet.create({
icon:{
height: 24,
width: 24
}
})
export default NoteMeHome;
之后,在运行项目时,在上述类中,当我单击任何抽屉项目转到另一个屏幕时,它会在控制台中显示以下错误 -
wanrning: can't call setState(or forceUpdate) on an unmounted component. This is no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asyncshronous tasks in the componentWillUnmount method.
我猜 AsyncStorage.getItem('token') 函数调用出了问题,因为如果我删除该函数,它不会显示任何警告。
所以,如果有人帮助我知道我应该在哪里调用以下代码,那就太好了-
AsyncStorage.getItem('token').then(value =>
//AsyncStorage returns a promise so adding a callback to get the value
this.setState({ getValue: value })
//Setting the value in Text
);
删除警告?
最佳答案
阿西夫
这就是我的想法:
import React from 'react';
import { StyleSheet, Text, View, Image, AsyncStorage } from 'react-native';
import {Icon, Button, Container, Header, Content, Left} from 'native-base';
import CustomHeader from './CustomHeader';
class NoteMeHome extends React.PureComponent {
state = {
text:'',
storedValue:'',
getValue: ''
};
static navigationOptions = ({navigation}) => ({
title: "Home",
headerLeft: <Icon name="ios-menu" style={{paddingLeft:10}}
onPress={()=>navigation.navigate('DrawerOpen')}/>,
drawerIcon:
<Image source={require('../assets/icon.png')}
style={styles.icon}
/>
});
componentDidMount() {
const token = await AsyncStorage.getItem('token');
this.setState({ getValue: token });
}
render() {
const {storedValue, getValue} = this.state;
return(
<Container>
<CustomHeader
title="Home"
drawerOpen={()=>this.props.navigation.navigate('DrawerOpen')}
/>
<Content contentContainerStyle={{flex:1, alignItems:'center',
justifyContent:'center', padding:10}}>
<Button full onPress={()=> this.props.navigation.navigate('Settings')}>
<Text style={{color:'white'}}>{storedValue}</Text>
</Button>
<Text>{getValue}</Text>
</Content>
</Container>
)
}
}
const styles = StyleSheet.create({
icon:{
height: 24,
width: 24
}
})
export default NoteMeHome;
我不知道在你的情况下你是否应该真正尝试处理组件的更新,因为你没有任何 Prop 。
问候,
关于javascript - 在哪里调用 AsyncStorage.getItem( ) 函数来加载react-native中保存的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340454/
我正在开发一个使用 Expo 创建的 React Native 项目。我一直在使用普通的旧 AsyncStorage , 从 react-native 导入,一切都很好。 在查找如何模拟 AsyncS
我正在使用 AsyncStorage在 React Native 中,但我的问题总是出错 _reactNative.AsyncStorage.setItem is not a function 当我使
出了什么问题: Execution failed for task ':app:multiDexListDebug'. > A failure occurred while executing com
我有一个基于卡片的应用程序(左右滑动) 当我向左或向右滑动时,我会为其分配功能。 SwipeRight:(向右移动时执行的功能) storeInLevel1(props.id) removeFromL
我已经查看了有关 asyncStorage 的文档,但不明白为什么它没有保存我的数据。我将 Laravel 用于 API 和 Vue native(react-native 的包装器)。当用户注册时,
我有一个带有记住我的按钮的登录屏幕,当我连接时,我想将此信息存储在 asynsctorage 中,如下所示: if (this.isFormValid()) { AccountSe
如果这个问题是语法错误或类似的问题,请原谅我,我是 React-Native 的新手,还有很多东西需要学习。 我按照文档尝试了 AsyncStorage,但它返回了一个错误。 class iCare
如何在“componentDidMount”处获取 AsyncStorage 项目,这里是我的代码 componentDidMount() { AsyncStorage.getItem('custom
我正在使用 expo 和 react-native我正在尝试使用 AsyncStorage 在持久存储中保存 3 个值这样我就可以从应用程序的任何地方获取它,但是 asyncstorage 不保存或检
我有一个 react-native我计划重新编写所有代码库的项目,新的源代码。我打算使用相同的包ID。 所以我的客户,他们希望用户会收到有关新应用程序版本的通知。在他们更新后,登录状态将保持 ,因此用
我正在使用 redux-persist在一个 react native 项目中,它在大量设备上运行得很好除了 Android 7 .我正在尝试调试为什么我的本地存储也不持久的问题,并发现了这一点: 以
我想在我的 React Native 应用程序中保留用户的帐户凭据(即用户名和密码)。我应该使用 AsyncStorage ? 换句话说,我想知道是否以及如何AsyncStorage保护其内容。 do
我正在尝试从异步存储中获取项目。 componentDidMount() { console.log('componentDidMount') AsyncStorage.getItem
我的应用程序从网络服务器下载图像并保存它们,但我不确定将它们保存在哪里,是否有图像的本地存储? 谢谢 最佳答案 试试 rn-fetch-blob 你可以 创建文件 删除文件 创建目录 读取文件 下载文
我无法使用 AsyncStorage setItem。以下是我的代码。因此,我从 TextInput 获取状态名称,然后将其存储在 TouchableOpacity 的 AsyncStorage on
我是 React 原生环境的新手,我正在尝试构建一个应用程序,我在其中使用 AsyncStorage 来保存用户首选项。 我无法保存/获取任何东西并且低于警告,例如, [Unhandled promi
有什么方法可以同步我通过asyncStorage获取的数据吗? 我在 BaseAction.js 中获取数据 import { AsyncStorage } from 'react-native';
我有一个react-native应用程序,我在其中调用一个api,它应该返回JSON,但我只是未定义。 export function fetchFromAPI() { AsyncStorage.
我试图用这个存储日期:https://github.com/react-native-community/datetimepicker 问题是我希望将日期保存在 AsyncStorage 中,可能还存
如何在 Asyncstorge 中设置多个值?因为我正在尝试设置 token和 user_id .这些是服务器响应值。 这就是响应的样子 json { error: 0, data: 'User
我是一名优秀的程序员,十分优秀!