I know how to read the json file just by importing it
import file from './config/data.json'; console.log(file);
.
But is there an easy way to write or edit on it.
Use AsyncStorage for saving local settings:
The following is to set your settings in your code (This example is for some Switch
async setSettings() {
try {
var obj = {};
var settings = await AsyncStorage.getItem('settings');
settings = JSON.parse(result);
Object.assign(obj, settings);
this.setState(obj);
}
catch(e) { }
finally { }
}
The following is to change your settings in your code
switchChanged(field, value) {
var obj = {};
obj[field] = value;
AsyncStorage.getItem('settings').then(function(strResult) {
var result = JSON.parse(strResult) || {};
Object.assign(result, obj);
AsyncStorage.setItem('settings', JSON.stringify(result));
});
this.setState(obj);
}
And finally the call at the render method
<Switch
onValueChange={(value) => this.switchChanged('reminders', value)}
value={this.state.reminders} />
Hope it can help you :)
If you absolutely want to read/write files you could use react-native-fs .
If you want to persist application specific settings I would recommend using AsyncStorage .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.