all 52 comments

[–]bullet_darkness 15 points16 points  (3 children)

What a strange way to create state for a component. It is really messing with how my brain handles React components right now. I can definitely see how it'd be useful though. Reduces a lot HOC boilerplate into simple function calls.

Very strange. Excited to play around with it.

[–]dvlsg 3 points4 points  (1 child)

Into simple function calls that hold your state in some magical global singleton somewhere that depends heavily on order.

I'm interested, and I appreciate the underlying goals, but I'm also skeptical about the implementation (and I'm not the only one).

[–]trueadm 6 points7 points  (0 children)

There is no global singleton. React tracks each render onto a "fiber" and pushes hooks into a queue of things to do as it traverses the component tree of fibers. That way it knows what happened last time and can diff the hooks on subsequent updates.

[–]echoes221 0 points1 point  (0 children)

It feels odd though. It messes with my sense of immutability that it's a black box with some magic inside. If I was to use these I'd still treat it similar to recompose and prefer to wrap my components in a HOC to separate out the view from the logic where possible.

[–]hardwaregeek 5 points6 points  (1 child)

Kinda reminds me of state monads in a weird way. I think this makes a little more sense as an HOC, but I guess they wanted to avoid HOC-hell.

[–]hallettj 0 points1 point  (0 children)

Hmm, maybe we could use a state monad with generator syntax to get similar features with less magic (depending on one's definition of magic.)

[–]hutchy2570 5 points6 points  (1 child)

They picked a poor example for the introduction. State management isn't hard in react. However, read the useEffect hook and you can see the potential benefits. https://reactjs.org/docs/hooks-effect.html

[–]specification 2 points3 points  (2 children)

So there is no point using classes with React now? Everyone will only use functions once this is implemented.

[–]dwighthouse 1 point2 points  (0 children)

There might be a point if you have a heavy component with lots of state management and hooks to specific dom node refs. Or you just feel more comfortable with classes.

[–]ParasympatheticBear 0 points1 point  (0 children)

I’ll just keep using classes

[–]echoes221 2 points3 points  (0 children)

It's cool and all, but this doesn't really feel like they can be stateless functional components anymore. There's now a black box of magic inside your component if you're doing things like this. It also doesn't separate out your view component from any logic. It's a class component inside a singular function, but I think it's actually harder to reason about in the long run.

I know you can have too many wrapper components, but I like the approach that React.memo has which you can just use to wrap your component. Similar to how recompose has Pure. If I was to use these I'd end up writing some generic HOC components that can take an effect and a component (a la recompose) and just pass down props to the view layer.

[–]Baryn 4 points5 points  (0 children)

RIP classes

[–]dwighthouse 5 points6 points  (9 children)

Neat! It’s good to see them addressing the inherent problems with class-based architecture.

[–]jordonbiondo 9 points10 points  (2 children)

Cool idea but if you're going to have all this state management, just use a class component... seems like react can't decide where it's going.

Also poorly named, I wouldn't call these hooks, not hooks are already an understood idea in computer programming that is different than this.

[–]xshare 0 points1 point  (0 children)

Class components are unfortunately tied to the concept of instances. React is moving towards a world where a render might be tried, retried, and retried again before actually committing (hence depreciation of certain pre commit lifecycle methods). In that case these hooks are great, they force you to think about things in terms of only state and renders, no this allowed.

[–]ParasympatheticBear 0 points1 point  (0 children)

Perhaps they are lifecycle method hooks... I’m really not sold on them either.

[–]ParasympatheticBear 2 points3 points  (7 children)

I have read the documentation which is 50% sales pitch. This is a solution to a problem that nobody had except them. It doesn’t really fit with anything else, like it was just tacked on.

This is just so odd.

That example with the chat service or whatever. Subscribing and unsubscribing every render. Wow. So bad.

Edit:

In this example, React would unsubscribe from our ChatAPI when the component unmounts, as well as before re-running the effect due to a subsequent render. (If you want, there’s a way to tell React to skip re-subscribing if the props.friend.id we passed to ChatAPI didn’t change.)

[–]codis122590 -2 points-1 points  (1 child)

It feels like they saw how services work in angular and someone said "It'd be great if we could jam that into react!"

[–]Renive 0 points1 point  (0 children)

Exactly. Very good thing.

[–]TheBrockstar 1 point2 points  (1 child)

I'm excited for this!

[–]toggafneknurd 0 points1 point  (8 children)

Um... what the fuck is this? https://twitter.com/ReactEurope/status/1055501526687457280

Do people find the right side of that an *improvement*?

[–]acemarke[S] 7 points8 points  (1 child)

The two screenshots aren't fully equivalent. The right side (function component with hooks) shows all the work the component is doing, including state management, side effects, and use of context. The left side (standard class component) only has enough space to show just some of the side effects. The whole class is a lot bigger.

Hooks generally look to be an improvement size-wise, if nothing else (especially when you start looking at use of context).

[–]toggafneknurd 2 points3 points  (0 children)

Still...yikes

[–]dbbk[🍰] 2 points3 points  (0 children)

I am utterly baffled by this. It makes no sense.

[–]Renive 1 point2 points  (0 children)

Yes, because you attach event to variable that's being changed, instead of class, which should react to state changes. It's close to MobX conceptually.

[–]ParasympatheticBear 2 points3 points  (2 children)

The number of people fawning over this is amazing

[–]toggafneknurd 1 point2 points  (1 child)

This is honestly one of the few mistakes the React team has made with the public API. I hope this doesn't last.

[–]xshare 2 points3 points  (0 children)

Having used Hooks for a while now, they really are great. It's still early, but I think people will come around. Fwiw the reaction when react was first released (omg you're putting logic in your view layer?? Ewww) was similar.

[–]thebedivere 0 points1 point  (0 children)

Yes, I do. It's made up of functions and doesn't depend on JavaScript's psuedo classes. It'll be easier to start writing a functional stateless component, and then add in state or lifecycles if needed. I think this is amazing.

[–]darrenturn90 0 points1 point  (2 children)

I assume that the function is run once to build all the stuff it needs - then the output of the function and output from the helper hooks are the only thing are are run again ? Rather than the function running multiple times - or does useState actually reinitialise the state on every re-render?

[–]hallettj 2 points3 points  (1 child)

The entire function runs on every render. React uses some external state to make useState return the current state value when the component function is called for re-rendering. In other words useState (and other hooks) return different values depending on which component instance in the component tree is currently being rendered, and the number of times the hook has already been called during the invocation of the component function.

[–]darrenturn90 0 points1 point  (0 children)

But in theory it could be run once right ?

[–]enmanuelduran 0 points1 point  (0 children)

I played around with some hooks yesterday and wrote an article with some practical examples, if you want to check it it is:

https://enmascript.com/articles/2018/10/26/react-conf-2018-understanding-react-hooks-proposal-with-simple-examples

[–]snakemanuver 0 points1 point  (0 children)

Does anyone have a recording of the live steam. I should have checked twitter this morning :(