Zygro
Zygro

Reputation: 7119

How can a react-native app save a variable after the app is turned off?

I want to make an app remember some data that a user puts in even after he turns off the phone (like auto-login). How can this be done in React Native? Or which libraries/components should I research?

Upvotes: 3

Views: 4337

Answers (3)

jasonmerino
jasonmerino

Reputation: 3240

If you want to do something like auto-login you should probably be using react-native-keychain so that you can store user credentials in the keychain on the device in a more secure way.

If you are looking to store a particular application state or other non sensitive data you could use react-native-simple-store which provides a really simple wrapper around AsyncStorage and takes care of the boilerplate JSON.stringify/JSON.parse that you need to do for objects and arrays.

Hope that helps!

Upvotes: 8

Nishanth Shankar
Nishanth Shankar

Reputation: 1976

You can use AsyncStorage bundled with react-native. It creates key value pairs

AsyncStorage.setItem("key", "value").done() // both key and value have to be strings AsyncStorage.getItem("key").then((value) =>{}).done()

You can check the documentation for more options https://facebook.github.io/react-native/docs/asyncstorage.html#content

Upvotes: 1

sunilp
sunilp

Reputation: 51

you can use flux stores, async store to store the current state in store, and re-route based on the store

can use componentWillMount mixing for setting up state before rendering view. for e.g.:

componentWillMount() {
    LocalStorage.bootstrap(() => this.setState({bootstrapped: true}));
},

you can refer https://github.com/brentvatne/flux-util/blob/master/lib/flux-util.js for example.

Upvotes: 1

Related Questions