Answer the question
In order to leave comments, you need to log in
Mobile first for react native, how best to implement?
Hey! You need an app that works offline.
Now it works like this:
1) data is loaded into state from AsyncStorage and rendered in render()
2) an asynchronous request is sent to the server and if the request is successful, then state is updated and rendered in render() and data is updated in AsyncStorage
The code is something like this
constructor() {
this.state = {
invoices: []
}
this.getStorage()
}
async getStorage() {
try {
let invoices = JSON.parse(await AsyncStorage.getItem('invoices'))
this.setState({
invoices: invoices
})
fetch(server_url).then(response => {
return response.json()
}).then(json => {
// AsyncStorage
storage.set('invoices', JSON.stringify(json))
this.setState({
invoices: invoices
})
})
}
}
Answer the question
In order to leave comments, you need to log in
https://github.com/rt2zz/redux-persist
this package persists the state of the store in AsyncStorage for you. Then you do not need to manually enter data into the store and it is much more convenient, but your method is also not bad.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question