Search code examples
react-nativeasyncstoragereact-native-fs

How to save in local AsyncStorage some text file?


I'm creating a new app with react native that fetch some users from a url.

Next, you can choose one of these users from a list and choose another data. Then you have to select a time.

For example: is like when you have a list of artist, then you choose an album and then a song.

I'm currently using AsyncStorage.

Thank you for helping.

I want to save all this data choosen in a text file and store it in the smartphone since you are online, and then upload this text file.

My question is how to save the data in the text file. The file need to be in this form:

  1. name: John Doe
  2. number: 12345
  3. time: 1,5

Solution

  • Using AsyncStorage, to store data

    await AsyncStorage.setItem('user', JSON.stringify({name: 'John Doe', number: 12345, time: '1,5'}));
    

    this will save as json string

    to store as separate items

    await AsyncStorage.setItem('name','John Doe');
    await AsyncStorage.setItem('number','12345');
    await AsyncStorage.setItem('time','1,5');
    

    note that value needs to be stored in string, you need to convert at retrival

    to get stored data

    const user = await AsyncStorage.getItem('user');
    

    or else for single element

    const name = await AsyncStorage.getItem('name');