News for hackers. Stuff that matters.

Ask HN: Can someone explain me React Hooks?

I find React hooks complex and hard to gasp. They feel too smart and over-engineered. When I write JavaScript I tend to think of simple functions with normal programming constructs. I'd rather use basic variables (on instances, localStorage, URL, etc..) rather than a weird beast like `useState(0)`. What is going on here? Am I stupid or something?
( 2 comments | on HN )
Re:Ask HN: Can someone explain me React Hooks? by acemarke on Saturday March 19, 2022 @12:01PM (#30734855)
Are you asking more about how hooks work internally, how to use them, or why they're designed this way?

I'd suggest reading these couple articles to understand more about the "how they work" and "how they're designed" aspects:

- https://www.swyx.io/hooks/

- https://overreacted.io/why-isnt-x-a-hook/

- https://overreacted.io/react-as-a-ui-runtime/

Re:Ask HN: Can someone explain me React Hooks? by Isammoc on Saturday March 19, 2022 @04:42AM (#30732212)
The whole purpose is the pureness of your component.

When react executes your component function, it will initialize a context (a big array) and each hook will be linked to an index in this context.

So, while being nearly pure, we can describe side effect but only for the provided context.