Now we're going to talk about React. Turns out, if you're not using your WSGI, this gets worse. Rich clients make everything worse. I mean, they make user experiences better, but they make things harder for us. Because now, say I'm looking at my list of messages, and I send a new message, and then I choose to revalidate. I choose to make an API request to get the new list of messages. So I can have a list of my messages in this channel that includes the one I just sent. But then I don't update the users. And now, if those two are out, they could be out of date for, what, 30 seconds or something. Once we're updating some data, but not all our data on the client, and that data is supposed to be joinable, it's no longer if it's now when.
Whenever we encounter a case where somebody sends a message and is no longer, you know, anyone that joined the channel in the last 30 seconds could cause that front end error. Once again, we really can't ignore this TypeScript error now. And live updates kind of make everything worse. Again, they make things better. But they make it more important for us to reason about consistency. Maybe you're periodically fetching new data. A nice way to do this is with React Query. And let's say that we're being really careful here. And React Query lets you use timers or tab visibility or manual invalidation logic to decide when to send those fetch requests, update the new data. And let's say, just be careful, we're invalidating everything. So we won't run into this messages are new, but users are old. They're all new all the time. But even if we fetch those and we fetch them at the same time, the requests could finish at different times. And now we're gonna have these little flashes in the user interface where, oh, we got the new users, let's render those. Oh, we got the new messages. And maybe they're not annoying flashes, but they are an indication of trying to render the new set of users with the old set of messages. So many renders, not necessarily flashes, but many renders could mean many opportunities for that inconsistent data to bite us.
If this sounds familiar, you maybe were using React back when we had to think more about unstable batched updates, when we had to say, you know what, I have two set states and they are not called as a direct result, a synchronous result of a user action. And so it is my job to note that and if I don't surround these with, if I don't batch these updates, the first set update will run first and I'll re-render and then the next one will run.
Comments