为什么AsyncStorage会使用react native返回“null”?

问题描述 投票:0回答:1

现在,我正在制作一个反应原生和世博的移动应用程序,并尝试使用AsyncStorage存储一组键和值。但是,我可以从AsyncStorage.get('key')获得“null”;

下面的代码是关于使用AsyncStorage的代码。

async checkQrUrl(data) {
const asyncStorageKey = JSON.stringify(data);

if (this.state.couponType === 'once') {
  try {
    if (data === this.state.UniqueQrUrl) {
      await AsyncStorage.setItem('qrcodevancoupon001shop', 'aaa');
      Alert.alert('一回切りのクーポンを使用しました');
    } else if (data !== this.state.UniqueQrUrl) {
      Alert.alert('QRコードが違います');
    } else {
      Alert.alert('予期せぬ障害が発生しました。前画面に戻って再度お試しください');
    }
  } catch (error) {
    console.log(error);
  }
}
}

除了这段代码,下面一个是获取密钥。

async componentDidMount() {
const { value } = this.props.navigation.state.params;
await AsyncStorage.getItem(`qrcodevancoupon001shop`, (result) => {
  this.setState({ couponModalStatus: result });

});

}

react-native expo asyncstorage
1个回答
1
投票

const value = await AsyncStorage.getItem('qrcodevancoupon001shop');

将它放在变量名中并返回它。

this.setState({ couponModalStatus: value });
© www.soinside.com 2019 - 2024. All rights reserved.