Skip to content

Add Hooks API #1247

Closed
Closed

Description

@kossnocorp

React team just announced new React Hooks API, and I think it will be a worthy addition to Preact.

The RFC: reactjs/rfcs#68
A blog post at React blog: https://reactjs.org/docs/hooks-intro.html

Activity

  1. developit commented on Nov 2, 2018

    @developit
    Member

    It looks like we're going to be releasing hooks support as an opt-in import, so you only pay for them if you use them. It'll look like this:

    import { useState } from 'preact/hooks'
    
    export function Demo(props) {
      const [count, setCount] = useState(0)
      return <button onClick={() => setCount(c => c+1)}>{count}</button>
    }

    There's been some discussion of this on Twitter - currently we have a working prototype.

  2. huozhi commented on Nov 3, 2018

    @huozhi

    @developit is there a current developing branch for hooks feature now?

  3. arusanov commented on Nov 8, 2018

    @arusanov

    What if all components stuff can also be moved into opt-in import?
    Like import { Component } from 'preact/component'
    So if you want to use only functional components and hooks you don't pay for component(Will|Did)...
    Sure it's a breaking change but maybe someday...

  4. hugmanrique commented on Nov 10, 2018

    @hugmanrique

    Webpack should already (don't quote me on this) just remove the Component class and all other functions you don't use in your code.

  5. SolarLiner commented on Nov 10, 2018

    @SolarLiner
    Contributor

    Only with the proper tree shaking plugins? Or is it part of Webpack now and I've just lost touch (last option most likely)?

  6. hugmanrique commented on Nov 10, 2018

    @hugmanrique

    Webpack 4 does tree shaking in production mode (Source: https://webpack.js.org/guides/tree-shaking/) 😀

  7. arusanov commented on Nov 11, 2018

    @arusanov
  8. AlexGalays commented on Nov 18, 2018

    @AlexGalays
    Contributor

    Moving components into their own sub-package sounds great, especially since they become useless if one go with hooks exclusively. So most projects could decide whether they want the OO style (/components) or the more FP style (/hooks)

  9. marvinhagemeister commented on Nov 19, 2018

    @marvinhagemeister
    Member

    @AlexGalays it's unlikely that they'll become "useless" anytime soon. There are a lot of third-party components on npm and it will take a long time for them to change. And that assumes that all the authors do want to change to hooks in the first place.

  10. AlexGalays commented on Nov 19, 2018

    @AlexGalays
    Contributor

    @AlexGalays it's unlikely that they'll become "useless" anytime soon. There are a lot of third-party components on npm and it will take a long time for them to change. And that assumes that all the authors do want to change to hooks in the first place.

    Yes, I never wrote that though.

  11. marvinhagemeister commented on Nov 19, 2018

    @marvinhagemeister
    Member

    @AlexGalays Oh sorry, my bad. I misread your comment.

  12. developit commented on Nov 20, 2018

    @developit
    Member

    In order to move Component into a subpackage we'd need to disentangle it from the renderer. Right now it's plumbed directly into the renderer since setState and forceUpdate enqueue rendering. It seems like the only way to remove that direct dependence would be to export a secret member from Preact that can be used to call enqueueRender(). This could potentially be done the same way we do hooks (import {options} from 'preact'; options.enqueueRender()). I'm not sure what the savings would be though.

  13. 16 remaining items

  14. marvinhagemeister commented on Jan 28, 2019

    @marvinhagemeister
    Member

    FYI: The hooks api will be part as an experimental feature of our next major release (titled: Preact X). The PR for it and a second one which optimizes bundle size was merged just before the weekend 🎉

  15. Anwardo commented on Jan 28, 2019

    @Anwardo

    FYI: The hooks api will be part as an experimental feature of our next major release (titled: Preact X). The PR for it and a second one which optimizes bundle size was merged just before the weekend 🎉

    Great news! Dare I ask if there is an ETA on the release of X? 👀It sounds like it's going to be amazing 😃

  16. marvinhagemeister commented on Jan 28, 2019

    @marvinhagemeister
    Member

    @Anwardo We're currently aiming for a beta/rc release around mid February. We'll post an announcement at that time with more info 🎉

  17. marvinhagemeister commented on Feb 6, 2019

    @marvinhagemeister
    Member

    Quick update: We're currently working on adding the last missing piece to hooks wich is the new context api via useContext. We originally didn't plan to include it in the initial release of Preact X but due to popular demand and widespread usage of createContext we decided to include it 👍 The PR's for it are already there and are awaiting review and some size reductions.

    The downside is that this will slightly affect the release date. March seems like a more realistic estimate right now 👍

  18. wizzard0 commented on Feb 20, 2019

    @wizzard0

    @marvinhagemeister umm is it possible to peek at the code somewhere? grepping for useState doesn't return anything in developit/preact, developit/* or marvinhagemeister/* :)

  19. marvinhagemeister commented on Feb 20, 2019

    @marvinhagemeister
    Member

    @wizzard0 Preact X currently lives in a private repo but will be merged soon into this one here. The switchover date is March 4th. See: https://mobile.twitter.com/marvinhagemeist/status/1097973028426788864 🎉

  20. marvinhagemeister commented on Mar 4, 2019

    @marvinhagemeister
    Member

    We just merged the PR for Preact X #1302 which includes the hooks addon. An alpha release should be available in a few hours 🎉

  21. xeoncross commented on May 1, 2019

    @xeoncross

    Any idea of when the hooks documentation / API reference will be live?

  22. marvinhagemeister commented on May 1, 2019

    @marvinhagemeister
    Member

    @xeoncross Updating our docs (and our site in general) is on our roadmap, but it's hard to tell when that will be finished. In Preact hooks work exactly the same as in React so their docs for hooks match perfectly. The only difference is the import: preact/hooks vs react 👍 💯

  23. dandv commented on May 22, 2019

    @dandv

    I think I'm running into a compatibility problem with react-wordcloud. Running the example from the readme in a new Preact 8.4.2 app fails with

    useRef is not a function

    After installing preact@next and adding import { useRef } from 'preact/hooks', the error becomes

    preact.js?10a9:1 Uncaught TypeError: Cannot read property 'default' of undefined
    at rankChild (preact-router.es.js?bff2:82)
    at prepareVNodeForRanking (preact-router.es.js?bff2:65)
    at Array.filter ()
    at Router.getMatchingChildren (preact-router.es.js?bff2:303)
    at Router.render (preact-router.es.js?bff2:326)

    Installing preact-compat didn't help.

  24. ForsakenHarmony commented on May 22, 2019

    @ForsakenHarmony
    Member

    you want preact/compat, not the old preact-compat

  25. dandv commented on May 22, 2019

    @dandv

    @ForsakenHarmony: after removing preact-compat and installing preact/compat, I get

    TypeError: Cannot read property 'replace' of undefined

    image

  26. ForsakenHarmony commented on May 22, 2019

    @ForsakenHarmony
    Member

    do you have the latest preact-router?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions