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.
Gorgeous dog in Essex desperate for a new home after being ignored for 3 years
'Nobody wants' new wheelie bins in Colchester as residents slam 'not fit for use' bins
Nicole Kidman reveals her worst onscreen kisser… and it's not ex Tom Cruise
Meta reveals four Broadcom-built custom AI chips, claims some outperform commercial silicon
Social networking giant Meta has revealed details of four previously unknown custom chips powering its AI services.…