0

I am implementing an app where I can navigate to other users profiles. Obviously, entering to a profile have a cost for the database request, and also slows the user experience (having to wait for the db response to show the data).

Here comes AsyncStorage, a local database to cache objects and data from the DB. This API is deprecated, and some people is using redux with redux-persist instead...

In my app I am not using Redux, so to manage my purpose, I suppose I have to use AsyncStorage. But, what about React Context? Currently I have implemented a context provider "CurrentUserContext" which have a "VisitedUsers" map, in which I put the users data when I enter their profile (so to avoid unnecessary queries to the database, I only have to see if the data of that user is in this map). So, if a user want to see the most freshed data, he will have to refresh (the profile screen has a pull-to-refresh mechanism). Also, this context's life ends when the app is exited.

My question is: Is this a good option to use context for this kind of purposes? I have never seen anybody doing this... That is why I am doubtful.

From my point of view it's fine, since you really only have a short map (which gets done in a very short time) in a context that will last until the user closes the app or logs out.

Be careful, I ask it as a question, since I really don't know if this is a good practice. It should not be understood as "personal opinion".

Thank you.

Victor Molina
  • 1,722
  • 2
  • 19
  • 1
    This may help you https://stackoverflow.com/questions/49568073/react-context-vs-react-redux-when-should-i-use-each-one – Guruparan Giritharan Nov 12 '20 at 09:50
  • 1
    Thank you!!!!!! – Victor Molina Nov 12 '20 at 10:17
  • @GuruparanGiritharan after reading it, I have the same question. The answers explain when to use redux before context API, saying that some of the redux features are not provided in the new Context API. But, in my use case, what I am doing is creating a kind of “HashMap of visited users” using the useRef hook in the Context Provider (to avoid unnecessary re-renders). So, if accessing data in a Map is fast, could this practice be wrong? – Victor Molina Nov 12 '20 at 14:30
  • The only cons I see is that data doesn’t persist in the local storage... but there is a pro, I avoid consuming device memory. For me, this seems like some kind of persistance layer, like a Catalog for examine, where all data is in memory. – Victor Molina Nov 12 '20 at 14:35
  • for example**** – Victor Molina Nov 12 '20 at 15:12
  • 1
    Yes it depends on your requirement, but reading from storage every time will have an impact – Guruparan Giritharan Nov 12 '20 at 16:35

0 Answers0