M
M
marrs2018-09-21 13:48:08
Mobile development
marrs, 2018-09-21 13:48:08

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
        })
      })
        }
    }

As you can see, there are no network checks and no network errors. It seems that I don’t see any problems with this method, a person will only see what is possible to get now and this in this particular case is better than seeing an error, but there is little experience in mobile development, I can’t understand something, who will advise what is better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2018-09-21
@marrs

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 question

Ask a Question

731 491 924 answers to any question