Live data from Hacker News

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

reactjs.org

121–130 of 181 posts

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

#121
post #41

Earlier quoted context omitted.

I don't know what is there to cheer about the introduction of a feature that offers nothing new.

Hooks offers nothing new over HoC and FaC the same way React offered nothing new over jQuery and EmberJS.

> Hooks offers nothing new over HoC and FaC

You've missed the point entirely. Hooks offer nothing new because their functionality was already implemented in React class components.

They literally add no functionality to React. The docs actually state quite clearly that developers are discouraged from replacing class components with function components.

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

#122

Hooks seem to be a drastic change in how we're going to write React components in the future. I'm quite satisfied with the current way of writing components which is to me is very explicit (with no magic). With Hooks React is taking a different direction from their original motto of explicit design patterns. From the looks of it, Hooks seems like a counter-intuitive design pattern but traditionally that's how most of…

In case you’re curious, I recently wrote up a deep dive on React from first principles that includes Hooks. https://overreacted.io/react-as-a-ui-runtime/ Personally I don’t see them as being either “magic” or “implicit”. You might find my post helpful for conceptualizing how they fit into the picture. (Warning: it is a longread. But it also explains 90% of React on a single page.)

I'm learning React and even though you prefaced it with "not beginner friendly", that was very helpful for understanding what goes on. Thanks!

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

#123
post #41

Earlier quoted context omitted.

Hooks offers nothing new over HoC and FaC the same way React offered nothing new over jQuery and EmberJS.

> Hooks offers nothing new over HoC and FaC You've missed the point entirely. Hooks offer nothing new because their functionality was already implemented in React class components. They literally add no functionality to React. The docs actually state quite clearly that developers are discouraged from replacing class components with function components.

"No new functionality" and "nothing new" are two different things. Improved developer experience, decreasing the surface area of bugs, etc - all examples of definite improvements that are not technically new functionality.

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

#124
post #41

Earlier quoted context omitted.

Hooks offers nothing new over HoC and FaC the same way React offered nothing new over jQuery and EmberJS.

> Hooks offers nothing new over HoC and FaC You've missed the point entirely. Hooks offer nothing new because their functionality was already implemented in React class components. They literally add no functionality to React. The docs actually state quite clearly that developers are discouraged from replacing class components with function components.

And what do you think React can do that jQuery cannot? Heck, what do you think React can do that standard HTML5/JS API cannot do?

React doesn't offer new functionality to do UI. It offer new ways to think about the problem.

Hooks doesn't offer any new functionality over `this.state` and component life cycle, it offser new way to think about the problem.

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

#125
post #41

Earlier quoted context omitted.

Hooks offers nothing new over HoC and FaC the same way React offered nothing new over jQuery and EmberJS.

> Hooks offers nothing new over HoC and FaC You've missed the point entirely. Hooks offer nothing new because their functionality was already implemented in React class components. They literally add no functionality to React. The docs actually state quite clearly that developers are discouraged from replacing class components with function components.

> The docs actually state quite clearly that developers are discouraged from replacing class components with function components.

Could you link to where it says this? My understanding is that hooks are intended to be the new best practice, and ideally all code would use hooks (which the React team believes are overall better than class components), but the React team doesn't want to force migration pain on everyone. So as I understand it, it's not that a class -> hook refactor is "discouraged", it's more "don't let this be too much of a distraction, and don't feel obligated to rewrite old code".

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

#126
post #106

Earlier quoted context omitted.

What's wrong with setstate? I have used it without problem.

There's nothing wrong with `setState`, in my opinion. However, it does do magic. Although it might feel like it immediately sets the state to be what you provide it as argument, it actually schedules a state update to be performed later when React feels like it. That's why it's best practice to provide an updated function rather than a plain object: if the new state depends on the previous one, and you read the previ…

> it actually schedules a state update to be performed later when React feels like it.

I thought it updates the state and the re-render is what is included in a scheduled function?

> it's best practice to provide an updated function rather than a plain object

I'm guessing you mean for a global store with shared state? Local components aren't singletons.

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

#127
post #79

Slightly off-topic, but has any of those who dislike hooks for the added magic seen Svelte.js version 3? It is ridden with magic. Top-level variables in the block are "observable" in the Vue sense of the word and accessible in the block; so whenever they change, the relevant parts of the template get updated. The creator of Svelte says that it's very intuitive, and that developer experience is great. I wonder what yo…

Not a fan of that non-JS svelte magic or the direction these frameworks are going. Fewer lines of code, true, but at the expense of understandability. I just want to get my work done, not learn yet another flavor of the day with non-intuitive implicit behaviors.

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

#128
post #106

Earlier quoted context omitted.

There's nothing wrong with `setState`, in my opinion. However, it does do magic. Although it might feel like it immediately sets the state to be what you provide it as argument, it actually schedules a state update to be performed later when React feels like it. That's why it's best practice to provide an updated function rather than a plain object: if the new state depends on the previous one, and you read the previ…

> it actually schedules a state update to be performed later when React feels like it. I thought it updates the state and the re-render is what is included in a scheduled function? > it's best practice to provide an updated function rather than a plain object I'm guessing you mean for a global store with shared state? Local components aren't singletons.

> I thought it updates the state and the re-render is what is included in a scheduled function?

See here: https://reactjs.org/docs/react-component.html#setstate

> setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state.

(Emphasis mine.)

> I'm guessing you mean for a global store with shared state? Local components aren't singletons.

No, local state, the one you set using `setState`. The offical docs don't actually recommend always using an updater function, but it's the primary method they explain, and my inclination is to better be safe than sorry:

> If the next state depends on the current state, we recommend using the updater function form, instead

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

#129
post #127
post #79

Slightly off-topic, but has any of those who dislike hooks for the added magic seen Svelte.js version 3? It is ridden with magic. Top-level variables in the block are "observable" in the Vue sense of the word and accessible in the block; so whenever they change, the relevant parts of the template get updated. The creator of Svelte says that it's very intuitive, and that developer experience is great. I wonder what yo…

Not a fan of that non-JS svelte magic or the direction these frameworks are going. Fewer lines of code, true, but at the expense of understandability. I just want to get my work done, not learn yet another flavor of the day with non-intuitive implicit behaviors.

> I just want to get my work done

I am generally with you, but the purpose of these frameworks _is_, first and foremost, to get work done (Svelte's creator Rich Harris says that it makes it possible to write UI components very fast). I love it when the code is explicit and when the framework does not introduce any weird shenanigans (Backbone, where are you? I am missing you), but this explicitness comes at a cost of having to write boilerplate.

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

#130
post #71

Earlier quoted context omitted.

But why does Hooks need to be implemented on top of functions and not classes? The way I see it is that Hooks today implements two distinct things: (1) a composable way to handle lifecycle and (2) a custom way to store state. Why can't we have the composable hooks available on React.Component like this? class Foo extends React.Component { constructor(props) { super(props) this.useEffect(…); this.useState(…); // or ma…

How would you pass values between them? Note Hooks execute on every render. That’s their whole point. I explain this here: https://overreacted.io/why-do-hooks-rely-on-call-order/#flaw...

I like judofyr's solution to this as well, but I think hooks that change the internal state of component are a bad idea, and breaks encapsulation in my mind.

I've used the hooks myself and I like the composability, but I can also understand the vitriol from the community. And so far my biggest complaint is that I can't use them in classes as well. I think if you treat them as a more core/composable type of object then you can use them in the same paradigm as Component classes.

My example here changes the names of the typical lifecycle events for clarity but you could use the component lifecycle names instead. Point is it should match the component lifecycle and allow the user to hook into each one separately but wrap everything up into a meaningful collection. A hook must be declaratively defined as a property in another class (I use the @annotation syntax here but I realize it's not standard yet), and that allows react to keep a consistent ordering on how/when hooks are called during lifecycle events. It also allows the hook to register itself for the lifecycle of the owner.

class UseWindowWidth extends Hook {

  // normal style of using state
  state = {
    width: this.props.value
  }

  // or maybe use state hook for new style
  //@hook(UseState, this.props.value)
  //width;

  handleResize = (evt) => {
    this.width.set(window.innerWidth);
  }

  hookDidMount() {
    window.addEventListener('resize', this.handleResize);
  }

  hookWillUnmount() {
    window.removeEventListener('resize', this.handleResize)
  }

  getWidth() {
    return this.state.width;
  }

  // other public methods here would allow owner to do things
  // ? although this doesn't fit well with top-down props style
}

class MyComponent extends Component {

  // @hook registers the hook into lifecycle events and
  // passes props to UseWindowWidth hook. Also, any changes
  // to hook state should cause a forceUpdate of the component
  @hook(UseWindowWidth, { value: window.innerWidth })
  windowWidth;

  render() {
    let width = this.windowWidth.getWidth();
    return (
      

Window width is {width}

); }
}

The 'props' concept might not make sense with the Hook object as I have it here, cause I can't see a nice way to declaratively update the props instead of just calling setters, but that might be ok for how these would be used.

I'm not in love with this yet, and there's some other things that would have to be ironed out (like coordination between hooks), but I think it opens some ways to allow hook use in class components as well.

Post reply on HN