Search code examples
javascriptreactjsreact-nativeasyncstorage

When i try to save a value with AsyncStorage in React Native, but when i try to retrieve the value, i have {"_U": 0, "_V": 0, "_W": null, "_X": null}


The code of my button is:

<Button style={styles.shopButtonStyle} onPress={async () => { await this.setState({'selectedShop': negozio['@id'] }); await AsyncStorage.setItem('@shop', "test"); }}>
          <Text>Scegli</Text>
        </Button>

And i go to another page with:

<Button style={styles.shopButtonStyle} onPress={async () => { this.props.navigation.navigate('Bookingstep2');  
        }}>
          <Text>Continua</Text>
        </Button>

In the new page i have:

componentDidMount(){
async () => { const shop = await AsyncStorage.getItem('@shop'); console.log(shop);  }

}

But the console is empty. Can anyone help me?


Solution

  • You should make your componentDidMount async instead and use try catch to check the error like:

    async componentDidMount() {
    try {
       const shop = await AsyncStorage.getItem('@shop');
       console.log(shop);
       } catch (error) {
        console.log(error); 
       }
    }
    

    There may be any error so you can check error in catch block