mirror of
https://github.com/morten-olsen/react-native-debug-console.git
synced 2026-02-08 00:36:26 +01:00
Added storage view, and updated the UI in general
This commit is contained in:
58
lib/src/components/data/Storage.js
Normal file
58
lib/src/components/data/Storage.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Component } from 'react';
|
||||
import { AsyncStorage } from 'react-native';
|
||||
|
||||
class Storage extends Component {
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {
|
||||
data: {},
|
||||
};
|
||||
this.update = this.update.bind(this);
|
||||
this.removeItem = this.removeItem.bind(this);
|
||||
this.clear = this.clear.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.update();
|
||||
}
|
||||
|
||||
async removeItem(name) {
|
||||
await AsyncStorage.removeItem(name);
|
||||
await this.update();
|
||||
}
|
||||
|
||||
async clear() {
|
||||
await AsyncStorage.clear();
|
||||
await this.update();
|
||||
}
|
||||
|
||||
async update() {
|
||||
try {
|
||||
const keys = await AsyncStorage.getAllKeys();
|
||||
const values = await Promise.all(keys.map(key => AsyncStorage.getItem(key)));
|
||||
const data = {};
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
const value = values[i];
|
||||
data[key] = value;
|
||||
}
|
||||
this.setState({
|
||||
data,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
children,
|
||||
} = this.props;
|
||||
const {
|
||||
data,
|
||||
} = this.state;
|
||||
return children(data, this.update, this.removeItem, this.clear);
|
||||
}
|
||||
}
|
||||
|
||||
export default Storage;
|
||||
Reference in New Issue
Block a user