0

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 });

});

}

4
  • const value = await AsyncStorage.getItem('qrcodevancoupon001shop'); Put it in the variable name and return it. Commented Apr 19, 2019 at 1:00
  • I did ! Thank you so much. But I am wondering why that is happened ? Commented Apr 19, 2019 at 1:05
  • The optional callback function type is void. Commented Apr 19, 2019 at 1:07
  • If my answer was helpful, please select the answer below. Commented Apr 19, 2019 at 1:08

1 Answer 1

2

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

Put it in the variable name and return it.

this.setState({ couponModalStatus: value });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.