Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: save user preferences

Native developers,

I really searched a lot but couldn't find anything that fits my needs.

I am new to react native and have a question. I wonder how I can save user preferences of my app.

For example, I am displaying a dismissible badge on my screen -> user dismisses it -> how do I save this decision so the badge won't appear on every start again?

I thought about writing a .json file where all preferences are defined and read it on every start.

Is this a common way to realize it or is there any other solution.

Thank you very much

like image 284
floriantaut Avatar asked Aug 09 '18 17:08

floriantaut


People also ask

Where are user preferences stored?

Usually to store user preferences, it is best to use localStorage. It does not have expiration so will be valid unless and until you clear it programatically or user clears it in browser manually .

What is shared preferences in react native?

Shared Preferences is the Android equivalent for a persistent key-value data store. Data in Shared Preferences is not encrypted by default, but Encrypted Shared Preferences wraps the Shared Preferences class for Android, and automatically encrypts keys and values.

How do I add a select option in react native?

To work with react-native-picker-select , we must import the RNPickerSelect component: import RNPickerSelect from "react-native-picker-select"; This component is then reused in our code to render the select view.


1 Answers

Updated Answer at February 2022

React native, officially deprecated the usage of its built-in AsyncStorage. The latest solution is to install the community package of it.

# Install via NPM
npm install --save @react-native-async-storage/async-storage

# ...or install via YARN
yarn add @react-native-async-storage/async-storage

# ...or install via EXPO
expo install @react-native-async-storage/async-storage

And the implementation is like this

import AsyncStorage from '@react-native-async-storage/async-storage';

const storeKey = '@storage_Key'
const storeData = async (value) => {
  try {
    await AsyncStorage.setItem(storeKey, value)
  } catch (e) {
    // saving error
  }
}


const getData = async () => {
  try {
    const value = await AsyncStorage.getItem(storeKey)
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

  • deprecated doc: https://reactnative.dev/docs/asyncstorage
  • async storage doc: https://react-native-async-storage.github.io/async-storage/docs/install/
  • Consider another options: https://reactnative.directory/?search=storage

Old Answer

There are so many options out there, but the most common you can use is the React Native built-in AsyncStorage.

Sample Code

import { AsyncStorage } from "react-native";
const storeKey = 'myPreference';
storeData = async () => {
  try {
    await AsyncStorage.setItem(storeKey, 'I like to save it.');
  } catch (error) {
    // Error saving data
  }
}

retrieveData = async () => {
  try {
    const value = await AsyncStorage.getItem(storeKey);
    if (value !== null) {
      // We have data!!
      console.log(value);
    }
   } catch (error) {
     // Error retrieving data
   }
}

read more at https://facebook.github.io/react-native/docs/asyncstorage.html

Or you could reconsider 3rd party library like:

https://github.com/kevinresol/react-native-default-preference

https://github.com/mCodex/react-native-sensitive-info

like image 125
I Putu Yoga Permana Avatar answered Oct 17 '22 05:10

I Putu Yoga Permana