That's something that will just make the app look more sophisticated, but there's also a security issue here. What if we had some user at the spa who was allowed to update the treatments, and they did something really wrong, and they got fired. We want to make sure that if they go back into the application, that we update their user data so that they are no longer authorized to update those treatments. It's really hard to get rid of a token in local storage, but React Query can make it so that we can update data, and we can make sure that we have updated data on startup.
Now, maybe you've swung over to the React Query should manage everything. However, we do need to look after a use auth in local storage. We need to hook those in somehow. Do we want those to subscribe and update the React Query cache individually? Then we've also got that chicken and egg situation for the user ID in the query function.
The solution that I have come up with is to decentralize everything with a used user hook. This is the source of truth. It tracks user data with an internal user state that's exposed as a return value of the hook and that is the canonical user data. We'll use local storage to maintain data not only across sessions, but also whenever a used user is initialized. We'll also keep the user data up to date with the server by using use query so that any of those triggers that I mentioned will update the data. We can take care of that chicken and egg situation by disabling that query if user is falsy. Whenever user data updates, it will go through this used user hook. In order to make sure that the data is maintained everywhere it needs to be in the internal user state, in the query cache, and in local storage.
Because I like visuals, here's the use user hook. It will both receive data from react query, when react query gets updated data from the server, and will also set data in the cache when it gets user data from other sources. Another source it might get user data from is the use auth hook, which receives data from sign in and sign out. Local storage is both getting set anytime user data is updated, and it's also providing the initial value for the use user hook. To get into that a little bit more, the use user hook is going to return that user state for people who want to subscribe to that user data, and then it's also going to return update user, an update user function that takes user data and updates the data for all three players. It will also have a clear user, which updates the data for all three players to be there is no user. Update user. And all of the updates, any user data updates go through use user. So when React query updates the data, it uses the update user function from the use user hook. Same with the use auth hook.
All right, so that's my brief introduction to how I solve this issue. If you'd like to look at the code, you can go to this GitHub repository and you can go to completed apps, lazy day spa, and client as far as the directory tree goes. I'll conclude this talk by talking a little bit about npm libraries that exist. The first one is React query auth.
Comments