Readit News logoReadit News
jaaamesey commented on React's useState should require a dependency array   bikeshedd.ing/posts/use_s... · Posted by u/jaaamesey
SebastianKra · 3 months ago
> Call it contrived, but maybe we want to add drawings to our tasks. With different brush colours

That's when you lift state up - either to the parent component, or put the value in local storage. Both as a user and as a developer, my mental model is that each item in the list has it's own form, rather than sharing one.

I haven't seen enough instances of this problem that I think it would warrant a change to the core api's. Maybe you could demonstrate more real-world examples?

A pattern that I sometimes like, is an internal state that is set only if the input is dirty. This has the advantage of preventing external updates from interrupting the user while they're in the middle of typing.

    function NumberInput({ value, setValue }) {
        const [tempValue, setTempValue] = useState(null)
        
        function submit() {
           const parsedValue = parseInt(tempValue)
           setTempName(null)
           if (parsedValue !== NaN) 
               setItemName(tempName)
        }
        
        return <Input value={tempValue ?? value} onChange={setTempValue} onBlur={submit} />
    }

jaaamesey · 3 months ago
I'd agree that generally, using `key` to reset state at the component tree level is fine, and that good component structure or persisting state through other means makes it a lot harder for this issue to come up.

> my mental model is that each item in the list has it's own form, rather than sharing one

100% - it makes things a lot easier to reason about if you're guaranteed to have a fresh instance of a component for each item being switched to. In fact, if I saw that stale state issue come up again in production, and it wasn't the kind of thing avoiding state entirely could fix, my suggestion would just be `key` instead of reaching for a third party hook.

I wouldn't be suggesting a change to React itself if that change also didn't enforce that way of thinking, albeit at the hook level instead of component tree level. `key` always felt like a weird escape hatch to me in that:

- it's not a solution lint rules would nudge someone towards

- `key` needs to be applied by the consumer of the component, not the component itself

- we lose all state in the tree and re-create all DOM nodes

I'll admit that it's hard finding good examples of where that last thing is an issue. The only other one off the top of my head might be preserving component/DOM state between route changes, e.g. keeping the same scroll position in a list of filters between similar screens for searching products. These issues only really come up when you have a deep component tree and lifting components up gets much trickier than having state reset naturally and atomically.

Deleted Comment

jaaamesey commented on Replicube: A puzzle game about writing code to create shapes   store.steampowered.com/ap... · Posted by u/poetril
jaaamesey · 4 months ago
Literally my favourite game of this year, thanks so much for making this!
jaaamesey commented on Show HN: Daily puzzle game where you build a bridge of your own words (SolidJS)   bridj.link/... · Posted by u/jaaamesey
sborra · a year ago
Very nice and polished looking.

Logic wise, it took me a while to get it. I thought that a bridge could be valid as long as every tile in it could be construed to form a word with the adjacent tiles. i.e. a word could be valid across bends and words could overlap on more than one tile.

I got the idea because the example has the words "T U N E" and "E A T" as part of the bridge, but it could also be interpeted as "T U N E" and "N E A T" with "N E" being part of 2 words.

Maybe you could adjust the example to avoid any ambiguity for first-timers?

Great work. Looking forward to tomorrow's puzzle :)

jaaamesey · a year ago
Thanks so much for the feedback! I'll see if I can do something to make that clearer.
jaaamesey commented on Show HN: Daily puzzle game where you build a bridge of your own words (SolidJS)   bridj.link/... · Posted by u/jaaamesey
ochre-ogre · a year ago
Cool game, I enjoyed playing it. Does it change midnight local system time or based on a particular timezone?
jaaamesey · a year ago
Thanks for giving it a shot! Puzzles should swap out at around midnight local time.
jaaamesey commented on Show HN: Daily puzzle game where you build a bridge of your own words (SolidJS)   bridj.link/... · Posted by u/jaaamesey
lovegrenoble · a year ago
I like this one: https://slices.ovh
jaaamesey · a year ago
Just played it, I'm a fan too (currently 30 puzzles in)

u/jaaamesey

KarmaCake day12May 22, 2024View Original