Katie Price appears in great spirits as she sight sees in Rome with pals after jetting off while her husband Lee Andrews remains stuck in Dubai
Adventure Island 'heroes' rescue mum and daughter stuck in 'waist-deep' sand
Beloved Colchester Zoo lion Bailey dies as keepers pay tribute
Trump declares war with Iran WON as Tehran fires intense missile barrage and vows to fight on
Dentist names the bowel cancer warning signs that appear in your mouth: 'Don't ignore bleeding gums, bad breath and a slimy white tongue'
Everyone is a winner on Ladies Day! Cheltenham revellers show off their moves on drink-lashed second day of horseracing festival
KATIE HIND: Will Angela of The One Show waltz into the top job on Strictly?
Greater Anglia train services to and from London stopped after trespass
Greater Anglia train services to and from London stopped after trespass
Britain's pothole crisis: Where's worst affected, which cars are most at risk and how can YOU make a claim for car damage?
Location, location, location! Empty plot of land goes on the market for £2.5million - because of where it's situated
Princess Diana made a remark I will never forget after Prince Charles kissed one of their nannies, writes RICHARD KAY in the Palace Confidential newsletter
Call police if you see missing 49-year-old man from Essex
Mandelson's '£75,000 bung for betraying Britain': Starmer's ministers 'happily' handed peer huge payoff... and the PM DID know he stayed friends with Epstein
New CCTV shows last sighting of missing Vitoria Figueiredo Barreto at heart of huge police search
Should Princesses Beatrice and Eugenie resign from their remaining charity patronages? Have your say in the Palace Confidential poll
Your Cheltenham cheat sheet to beat the bookies today, why it looks like the end for legendary Galopin Des Champs and dream comes true for Final Orders syndicate - CHELTENHAM BREAKFAST
GFiber and Astound Broadband To Join Forces
Read more of this story at Slashdot.
'Time to come clean on what you're hiding, the public need to know', top Tory demands as Mandelson files are released
CodeSOD: Awaiting A Reaction
Today's Anonymous submitter sends us some React code. We'll look at the code and then talk about the WTF:
// inside a function for updating checkboxes on a page if (!e.target.checked) { const removeIndex = await checkedlist.findIndex( (sel) => sel.Id == selected.Id, ) const removeRowIndex = await RowValue.findIndex( (sel) => sel == Index, ) // checkedlist and RowValue are both useState instances.... they should never be modified directly await checkedlist.splice(removeIndex, 1) await RowValue.splice(removeRowIndex, 1) // so instead of doing above logic in the set state, they dont setCheckedlist(checkedlist) setRow(RowValue) } else { if (checkedlist.findIndex((sel) => sel.Id == selected.Id) == -1) { await checkedlist.push(selected) } // same, instead of just doing a set state call, we do awaits and self updates await RowValue.push(Index) setCheckedlist(checkedlist) setRow(RowValue) }Comments were added by our submitter.
This code works. It's the wrong approach for doing things in React: modifying objects controlled by react, instead of using the provided methods, it's doing asynchronous push calls. Without the broader context, it's hard to point out all the other ways to do this, but honestly, that's not the interesting part.
I'll let our submitter explain:
This code is black magic, because if I update it, it breaks everything. Somehow, this is working in perfect tandem with the rest of the horrible page, but if I clean it up, it breaks the checkboxes; they're no longer able to be clicked. Its forcing React somehow to update asynchronously so it can use these updated values correctly, but thats the neat part, they aren't even being used anywhere else, but somehow the re-rendering page only accepts awaits. I've tried refactoring it 5 different ways to no avail
That's what makes truly bad code. Code so bad that you can't even fix it without breaking a thousand other things. Code that you have to carefully, slowly, pick through and gently refactor, discovering all sorts of random side-effects that are hidden. The code so bad that you actually have to live with it, at least for awhile.