gpt4 book ai didi

javascript - 在哪里调用 AsyncStorage.getItem( ) 函数来加载react-native中保存的数据?

转载 作者:行者123 更新时间:2023-12-02 23:56:50 24 4
gpt4 key购买 nike

我已在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com