I have following react native app structure (simpilifed)
- /MyApp
- /ios
- data.json
- index.ios.js
So, i need to access and write into data.json file. To access them, i can import Data from './data.json'
.
But how can i write into this file? I seen react-native-fs (https://github.com/johanneslumpe/react-native-fs), but i can not access the file in my MyApp
directory.
How do i store files like my data.json
, so i can write and access them on my iphone?
My data.json
file contains a bigger list of vocabulary, like
{
"words": [
{"word": "xxx", "done": false} // and more...
]
}
You can write into the file by react-native-fs. But the write access file path is depend on your phone platform like ios or android.
var RNFS = require('react-native-fs');
// create a path you want to write to
var path = RNFS.DocumentDirectoryPath + '/test.txt';
// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
console.log('FILE WRITTEN!');
})
.catch((err) => {
console.log(err.message);
});
This is the way to create new file and write your data. If you want to write data append file you need read the data and create new string write back to the file. Or you modify the Java and Ojb-c code yourself to create a new api to write append.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With