question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsyncStorage not working properly

See original GitHub issue

Current behavior

AsyncStorage not working properly on 0.60.5 I set and get the async storage with the help of two button. Value successfully sets and gets but when i reload the application and tried to get the value again without setting it return null.

Expected behavior

Async save the set value before and don’t remove it until we manually set it to null. But now every time application is reloaded it forgets the saved async value and i have to set it again and again. Basically setup my authentication with asyncstorage, if user is signed in it shows dashboard else login screen. But now user login and open app again, it shows login screen again and again. It was working fine on Android 8.1 with React Native 0.59. But i upgraded my project to 0.60, its behavior changed.

Code

export default class LibTest extends Component {
    static navigationOption = {
        title: "LibTest", header: null, headerVisible: false
    };

    constructor(props) {
        super(props);
        this.state = {
            asyncVal: null,
            storedData: "myValue"
        }

    }

    async componentDidMount() {

    }

    storeData = async () => {
        console.log("inside storeData")
        try {
            await AsyncStorage.setItem('Test', 'TestValue')
        } catch (e) {
            console.log(e)
        }
    }

    getData = async () => {
        console.log("inside getData")
        try {
            const value = await AsyncStorage.getItem('Test')
            this.setState({ storedData: value })

        } catch (e) {
            // error reading value
        }
    }

    render() {
        return(
            <View style={{ marginTop: 40 }}>
                <Text> {this.state.storedData}</Text>
                <Button title={"storeData"} onPress={this.storeData}><Text>storeData</Text></Button>
                <Button title={"getData"} onPress={this.getData}><Text>getData</Text></Button>
            </View>
        )
    }
}

Environment

  • Async Storage version: 1.6.1
  • React-Native version: 0.60.5
  • Platform tested: Android 9

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:31 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
darcoolacommented, Apr 28, 2020

@Krizzu I found bug in my code, thanks for your help, and sorry for trouble. The problem was that I checked the state in one of my Context which was not yet updated with the value stored in AsyncStorage while first rendered.

1reaction
krizzucommented, Dec 5, 2019

when i set any value to null, its ok but after a refresh everything rolls back…

Setting values to null/undefined is not supported - probably the error is thrown there, which you don’t handle. If you want to remove value, use .removeItem method

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async storage not working to set or get on React Native
Im trying to make the user authentication using async storage to decide which screen will be rendered so when i get the ...
Read more >
AsyncStorage - React Native
AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of ...
Read more >
How To Use React Native AsyncStorage | Jscrambler - Medium
AsyncStorage is a simple, asynchronous, unencrypted by default module that allows you to persist data offline in React Native apps. Learn more about...
Read more >
React Native AsyncStorage - Asap developers
This proves it's really working. Now that we know it works, it's time to store and use our application theme properly. Replace your...
Read more >
Building offline React Native apps with AsyncStorage
An end-to-end tutorial showing how to build an offline to-do list ... Since there's no data stored right now, the text after the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found