Race Conditions

Loading "Race Conditions"
πŸ‘¨β€πŸ’Ό Networks are unpredictable. Let's imagine the user clicks around a lot and that triggers several network requests:
*click* ------ request --------> update ui
  *click* ------ request --------> update ui
    *click* ------ request --------> update ui
That works nicely. But sometimes the requests come back in a different order for whatever reason (network latency, server load, etc):
*click* --------- request ----------> update ui
  *click* ---- request ------> update ui
    *click* ----- request -------> update ui
This would be a pretty bad user experience because the UI would be updating in a different order than the user clicked.
What would be better is if we avoid updating the UI if there's a newer request going out.
This is easier than you might think:
const latestNav = useRef(null)

// when we start a navigation:
const thisNav = Symbol()
latestNav.current = thisNav

// when we're ready to update the UI:
if (latestNav.current === thisNav) {
	// update the UI
}
That way any navigation that comes back out of order will be ignored.
πŸ§β€β™‚οΈ I've added a bit of code in to allow you to simulate a race condition. Do a search for "star" and you'll notice after two seconds, the URL gets updated to "?search=st" because we have a delay for that search term. When you're finished, this should not happen.
Go ahead and make that magic happen!
Login to get access to the exclusive discord channel.
  • General
    Failed to fetch dynamically imported module...
    FacuPerezM:
    I am getting this error out of nowhere, in the 'test' tab inside the Workshop app. ``` Failed to f...
    1 Β· 12 hours ago
  • General
    Runninng Learning app on iPad?
    marcus_polonus πŸš€:
    Hi all, Did anyone managed to setup learning app on iPad? I tried to use codesandbox, but when I am...
    0 Β· a day ago
  • General
    Clicking an exercise file launch VIM instead of VS Code
    DBattou πŸš€:
    I started going through the workshops, everything is setup and working fine except for opening a fil...
    • βœ…1
    2 Β· 5 days ago
  • General
    Epicshop not saving the video resolution
    Toni πŸš€:
    Playback speed and caption settings are saved, but the video resolution always defaults to auto. Is ...
    • βœ…1
    7 Β· 2 days ago
  • General
    Launch editor error: spawn code ENOENT
    Andrew Elans 🌌 πŸš€:
    Clicking index.html gives this error in VS Code. Both from Chrome and Safari. Anyone?
    • βœ…2
    3 Β· a day ago
  • General
    Welcome to EpicReact.dev! Say Hello πŸ‘‹
    Kent C. Dodds β—† πŸš€πŸ†πŸŒŒ:
    Welcome to the first of many posts in the EpicReact.dev channel! Take a moment to introduce yourself...
    • πŸš€5
    34 Β· 19 hours ago