Live data from Hacker News

With React 16.8, React Hooks are available in a stable release

reactjs.org

11–20 of 181 posts

Re: With React 16.8, React Hooks are available in a stable release

#11

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

To add to the other answers, the type-ability of hooks is so much nicer. useContext means I'm going to be using context a lot more in TypeScript code now that I don't have to add a bunch of type definitions to every component I want to use it in.

Re: With React 16.8, React Hooks are available in a stable release

#12

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

You should think about it in is this way - class components do not actually control its state. Using classes is just a convention supported by React that allows components to express their intention about what state they should have and how it should be updated, so it is just a form of interface. And hooks is a different way to interact with same underlying subsystem of the React, but they allow you to abstract, reuse and compose the logical parts of the interaction with the state.

Re: With React 16.8, React Hooks are available in a stable release

#13

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

Not specific to React, but with CommonJS modules you can use module scope instead of global scope to get private methods and variables, which the function can access via the closure.

    var foo = 1;
    var bar = x => x++;
    module.exports = function baz() { return bar(foo) }
I find this much more simple then classes. I even consider it an anti-pattern to make a class for something this simple.

Re: With React 16.8, React Hooks are available in a stable release

#14
post #10

Earlier quoted context omitted.

I've been working with hooks for quite a while now; its main benefit is composability - before hooks up you rely on Higher order components (HOCs, HOFs but in component rendering) to provide composability of component behaviour - this is messy, very messy, and not performant. By using hooks the logic tidies up massively (so working on it becomes a lot quicker) and several hard to diagnose performance bottlenecks disa…

Can you please give some example of behaviour that you share across components that couldn't be factored out into a function?

https://overreacted.io/making-setinterval-declarative-with-r... is the article that solidified hooks as a useful concept for me.

Re: With React 16.8, React Hooks are available in a stable release

#15
post #10

Earlier quoted context omitted.

I've been working with hooks for quite a while now; its main benefit is composability - before hooks up you rely on Higher order components (HOCs, HOFs but in component rendering) to provide composability of component behaviour - this is messy, very messy, and not performant. By using hooks the logic tidies up massively (so working on it becomes a lot quicker) and several hard to diagnose performance bottlenecks disa…

Can you please give some example of behaviour that you share across components that couldn't be factored out into a function?

They can always be factored out into a function, after all, both HOCs and hooks are functions. One big (or at least obvious) benefit of hooks over HOCs is the fact that they can be composed without the possibility of prop name conflicts.

Re: With React 16.8, React Hooks are available in a stable release

#16
post #4

Never a boring day being a frontend developer. Looking forward to add hooks to my React project. The next frontier would probably be functional strongly typed languages, like Reason or some variation on Elm?

A variation on Elm? Why not just Elm?

Well, he said "the next frontier". Probably implies that Elm didn't catch on, and means "what will the next major framework/paradigm/fad will be post-React".

Re: With React 16.8, React Hooks are available in a stable release

#18

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

For the vast majority of web apps, you’re going to need state that changes over time. Making individual components immutable is a nice architecture choice for several reasons, and hooks doesn’t really change that. Hooks just provide a way to plug state (which can change over time) into your components, which is something you already had to do. Where you were previously using class components, HOCs, or render props, you can now optionally use hooks, which have some well-documented advantages.

Re: With React 16.8, React Hooks are available in a stable release

#19

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

[deleted]

Re: With React 16.8, React Hooks are available in a stable release

#20
post #13

I don't get it. I thought the main appeal of functions were that they were stateless so you had immutability. We already have classes if we want state, so why is this an improvement? Functions have slightly less overhead maybe? But then why didn't the React team just work on making classes have less overhead? It seems sort of "extra".

Not specific to React, but with CommonJS modules you can use module scope instead of global scope to get private methods and variables, which the function can access via the closure. var foo = 1; var bar = x => x++; module.exports = function baz() { return bar(foo) } I find this much more simple then classes. I even consider it an anti-pattern to make a class for something this simple.

That gets you a single instance, but the whole point of React components is that you can use them lots of time and each will do its own instance of the thing.
Post reply on HN