News for hackers. Stuff that matters.
Re:React: You Might Not Need an Effect by nightpool on Thursday March 23, 2023 @12:12PM (#35277018)
Well, in the first place, this code snippet has nothing to do with useEffect, so I'm not sure why you're bringing it up. They have a separate page that they linked to that explains this pattern in more detail: https://react.dev/reference/react/useState#storing-informati...

But to summarize that page briefly, useState will always return the current value of the state and a function to set it. But "items" here is being used as the default value that's provided the very first time the component is rendered, when the state doesn't exist yet. In subsequent renders, the state exists, so "prevItems" will reflect the last value that was set with `setPrevItem`, not the current value of `items`

Re:React: You Might Not Need an Effect by jve on Thursday March 23, 2023 @04:05PM (#35280547)
I did read the other page and google around... but you and the other comment nailed it for me, thanks.