Hacker News new | past | comments | ask | show | jobs | submit login

So, let me get this straight.

- These are "just function calls"

- Except they are "iterators"

- Except if you have them in a conditional, you must provide an `else` branch so that `next` is called the same number of times

None of this is in the semantics of the language where I don't have to provide an else branch to an iterator, and function calls are not iterators ;)




Like I really don’t know how to explain this any clearer.

* React stores the state for your hooks in a list which is iterable.

* useBlah is just a normal function that internally calls next() on that list to get the next hook state which is why order matters.

* React checks that the iteratior on the list is at the end to see if you’ve consumed all the hooks and throws an error if not because it’s indicative of a bug in your code.

Like at this point the only thing I can recommend is try writing a toy implementation of hooks and see that they’re not magic, they’re just normal JS, and that the restrictions flow naturally from the implementation.

Like why do you want so bad for hooks to be weird?


> is just a normal function that

you keep showing isn't a normal function :)

> Like why do you want so bad for hooks to be weird?

I don't want them to be weird. They are weird already. https://news.ycombinator.com/item?id=30801466


I guess we have very different definitions of "semantics" then. Rspec is wild but it's still plain Ruby. I'll give you that it's not idiomatic and it comes with some unusual restrictions on its use but libraries have been doing forever, I guess I'm just used to "you have to call this function to init the lib", "this function has to come after this", "you must call the cleanup macro here", "importing this lib monkey-patches these classes."




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: