ryonz
ryonz

Reputation: 471

Why AsyncStorage will return "null" with react native?

Now, I am making a mobile app with react native and expo and try to use AsyncStorage to store a set of key and value. However, I can get "null" from AsyncStorage.get('key');

The code below is my code about apart of using 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);
  }
}
}

In addition to this code, below one is to get key.

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

});

}

Upvotes: 0

Views: 1363

Answers (1)

hong developer
hong developer

Reputation: 13906

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

Put it in the variable name and return it.

this.setState({ couponModalStatus: value });

Upvotes: 2

Related Questions