boulderer douze
boulderer douze

Reputation: 21

Best way to save json data and files in react native?

I am developing a react-native application. I want it to get a json file online and print it as a list, showing the image and by clicking, showing the pdf. Here is my json :

[
   {
      "id":"1",
      "nb_edit":0,
      "nom":"Catalog 1",
      "description":"test",
      "apercu":"img\/1.png",
      "pdf":"pdf\/1.pdf"
   },
   {
      "id":"2",
      "nb_edit":"0",
      "nom":"Hi",
      "description":"yes",
      "apercu":"img\/2.png",
      "pdf":"pdf\/2.pdf"
   }
]

It actually works fine. But now, I want to add an offline mode. For this, I want to basically set a version variable in the application to 0, and check if the version on the website (version.txt) is the same. If yes, just load the json file saved in the phone (the png and pdf are saved too). If not, download the json, update the json saved locally, update the version variable, and then load the files from the phone. Do you have an idea of how could I do? I thought about using redux-persist for the version, but will it work for the json, the images and the pdfs, and how? Thanks for your help.

Upvotes: 2

Views: 5964

Answers (2)

Oyeme
Oyeme

Reputation: 11225

You can use :

Option1:

AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.

https://reactnative.dev/docs/asyncstorage.html

Option2: Local database

SQLite is an open-source SQL database that stores data to a text file on a device. It supports all the relational database features. In order to access this database, we don’t need to establish any kind of connections for it like JDBC, ODBC.

react-native-sqlite-storage https://www.npmjs.com/package/react-native-sqlite-storage

Upvotes: 2

I would not try to save the file to the device. Instead of trying to save the file, I would save data to the device's storage by using AsyncStorage (If the file is not that big). If you go in this way, do not forget to stringify the object while saving the object to the storage.

You may use react-native-fs for saving files such as pdf, images.

Upvotes: 2

Related Questions