React-Native:AsyncStorage.getItem 不工作。我已经从 react-native-community 安装了 AsyncStorage
React-Native: AsyncStorage.getItem not working. I have installed AsyncStorage from react-native-community
我有一个屏幕,上面有 select 游戏玩家数量的按钮。单击这些按钮中的任何一个都会调用一个函数,将适当的数字传递给该函数,将该数字存储在异步存储中,然后导航到下一个屏幕。请参阅下面的代码:
//Example of the buttons:
<Text style={styles.text}>
How Many Players?
</Text>
<PrimaryButton
onPress={() => this.startRound(3)}
label="3"
>
</PrimaryButton>
<PrimaryButton
onPress={() => this.startRound(4)}
label="4"
>
</PrimaryButton>
//The function:
startRound = (num_players) => {
AsyncStorage.setItem('Num_Players', num_players);
this.props.navigation.navigate('player_names_screen');
}
在下一个屏幕上,如果我尝试使用 AsyncStorage.getItem 来获取这个数字,以便我可以用它做一些事情,我只会得到 null。请参阅下面的代码:
constructor (props) {
super(props);
this.state = {
get_players: null
}
}
componentWillMount = () => {
this.getNumPlayers();
var players = this.state.get_players;
alert(players);
}
getNumPlayers = async () => {
let players = await AsyncStorage.getItem('Num_Players');
this.setState({get_players: players});
}
我正在使用警报功能查看使用 AsyncStorage.getItem 得到的结果,正如我之前所说,它显示 "null"。正如标题中所说,我已经从 react-native-community 安装了 async-storage 并正在使用 "import AsyncStorage from '@react-native-community/async-storage';".
AsyncStorage 无法存储整数值以将整数值转换为字符串
AsyncStorage.setItem('Num_Players', JSON.stringify(num_players));
或者
AsyncStorage.setItem('Num_Players', ''+num_players);
请检查这个
https://snack.expo.io/@vishal7008/convert-variable-stored-in-asyncstorage-then-state-to-number
我有一个屏幕,上面有 select 游戏玩家数量的按钮。单击这些按钮中的任何一个都会调用一个函数,将适当的数字传递给该函数,将该数字存储在异步存储中,然后导航到下一个屏幕。请参阅下面的代码:
//Example of the buttons:
<Text style={styles.text}>
How Many Players?
</Text>
<PrimaryButton
onPress={() => this.startRound(3)}
label="3"
>
</PrimaryButton>
<PrimaryButton
onPress={() => this.startRound(4)}
label="4"
>
</PrimaryButton>
//The function:
startRound = (num_players) => {
AsyncStorage.setItem('Num_Players', num_players);
this.props.navigation.navigate('player_names_screen');
}
在下一个屏幕上,如果我尝试使用 AsyncStorage.getItem 来获取这个数字,以便我可以用它做一些事情,我只会得到 null。请参阅下面的代码:
constructor (props) {
super(props);
this.state = {
get_players: null
}
}
componentWillMount = () => {
this.getNumPlayers();
var players = this.state.get_players;
alert(players);
}
getNumPlayers = async () => {
let players = await AsyncStorage.getItem('Num_Players');
this.setState({get_players: players});
}
我正在使用警报功能查看使用 AsyncStorage.getItem 得到的结果,正如我之前所说,它显示 "null"。正如标题中所说,我已经从 react-native-community 安装了 async-storage 并正在使用 "import AsyncStorage from '@react-native-community/async-storage';".
AsyncStorage 无法存储整数值以将整数值转换为字符串
AsyncStorage.setItem('Num_Players', JSON.stringify(num_players));
或者
AsyncStorage.setItem('Num_Players', ''+num_players);
请检查这个
https://snack.expo.io/@vishal7008/convert-variable-stored-in-asyncstorage-then-state-to-number