16
leksyib
5y

React developers, What do you think about the new hooks api?

Will you refactor your codebases to use functional components or just stick with classes (even though the react community is moving towards removing classes completely)?

I think its awesome as it reduces the bundle sizes if you use function components though. I have been working on an awesome project for a while and I'm being tempted to refactor the whole codebase to use functions instead of classes. What do you think?

Comments
  • 5
    I'm no react user but I do like your font
  • 0
    I also think how it looks sucks when you have about 5-6 different data in states while using the new hooks
  • 1
  • 4
    I will not refactor what is already there. I will however use it in newly created components or projects.

    Sidenote: I am already living a classless life because in ReasonReact you only have functions and modules. And it compiles down to ES5 which is also without classes.
  • 0
    @finiteAutomaton oh cool! You won't get hit hard if react decides to remove classes. But I've never tried reasonReact. What perks do you get with reasonReact? (I know bundle size will be smaller since they're just funcions)
  • 2
    @leksyib
    Okay, as I am not sure if you really know what I am talking about, a small Primer:

    * ReasonReact is the glue library to write React in ReasonML which is a JS-developer friendly syntax for the OCaml language.
    * ReasonML provides you with a 100% covered type system, better than flow and typescript (because soundness is no goal of typescript)
    * It also has some smart features JS will probably get in decades, like pattern matching which is one of the things you did not know you needed before you used it. Also, it's immutable by default.
    * ReasonReact brings also reducer functionality included to the table, (also Side-Effects) so in general you need many fewer dependencies.
    * To import and use JS libraries and ReactJS components in ReasonML, you have a bunch of tools from the bucklescript project, generally the interoperability with JS is good.

    That being said, it is an all different language to learn, and it is mainly functional, but imho it is worth it.
  • 1
    @finiteAutomaton sounds cool, thanks! I'll definitely check out reasonML in my free time
  • 3
    They look interesting!
    I'm about a year behind in React news, so I'll have to start fresh, I fear.
  • 1
    COMIC SANS ON IDE?
  • 3
    I work at Facebook and we have been using it in our code base for a while.

    General sentiment towards hooks are positive. There are many benefits of hooks which you can find on the React website.

    However, the React team emphasizes that you would be better off not rewriting your whole app with hooks for these reasons:
    1. It's still experimental, APIs may change in future
    2. It takes effort to rewrite your app, and the marginal benefits are questionable. Hooks are great when writing new code because they are shorter but if you have already written code in classes, you have paid the cost and rewriting it wouldn't help much.

    There's one reason to rewrite though - smaller bundle size because classes (which lifecycle method names can't be transpiled) are now gone from the bundle.

    I would advise you to try it out but not commit too much to it. Wait for it to be more stable and for best practices around using hooks to emerge before rewriting your whole app in it.
  • 1
    @yangshun great, thanks!!
  • 1
    I think hooks are great, taking off a lot of boilerplate. Quite a shift in a way we’re gonna write React components.
  • 1
    @maushax Yeah I read that, but I'm sure the maintainers are working hard to fix that!
  • 0
  • 1
    Wtf is that? It's like an array of a class that is also a method.
  • 1
    @dangerzone haha nope it's destructuring.

    In es6, if there's an object = {a:1, b:2 }.

    I can do

    const { a, b } = obj

    And use a and b as separate variables.

    So that's how this works, you destructure the state's name and the function to change that specific state from useState.
  • 0
    My eyes... Change font asap holy shit
  • 1
    @leksyib that's a long way of saying "a class".

    So the reinvented wheel is called "destructuring". Gotcha.
  • 0
    @maushax Yeah, you need to git gud at writing bindings if you want to make a serious project, because even if there are bindings they may already be outdated syntax, or only partially.

    So it is sensible to create your own bindings but only for the features you actually use.

    That is probably the biggest hurdle in learning Reason/Bucklescript.
Add Comment