Search code examples
jsonreact-nativelocal

What would be the best way to write JSON in a locally stored file in React Native


Im currently working on a new design for a mobile app (only frontend). In the project I need to use states to change the css of a button. If the button has changed, next time the app is refreshed the state should be as you left it.

That is why I have a locally stored JSON file that is structured the same as how the apps current database is. Reading it is no issue, but I can't get the writing to work.

How I read the JSON:

const jsonData = require('../data.json')

function GetBaseState(id){
    console.log(jsonData.bases[id].state)
}

How would I go about changing that state in the JSON file?


Solution

  • In order to both of reading and writing Json file , you are able to use react-native-fs. For example by readFile method you can read json file and by writeFile method writing your json file in local storage. react-native-fs have good documents that you are able to reading that for more information and usage.