New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment how React hooks can improve our codebase #11161

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
4 participants
@youknowriad
Copy link
Contributor

youknowriad commented Oct 27, 2018

React 16.7 introduces this concept of hooks. In this PR, I'm creating some hooks to explore this API and how it can improve or not our codebase and APIs.

So far, I'm thinking that it's a great API, I like how you can reason about discrete behaviors and combine them later in components without having to wrap your components or create extra DOM nodes you don't really need.

  • See how I created two simple hooks useImageSize to retrieve the image size and height, useElementSize to retrieve the size of an element and refresh it if the window is resized.
  • See how I refactored our current ImageWidth component to combine the two hooks above instead of a complex component class.
  • I think the ImageWidth component can be eliminated entirely (in favor of using the hooks directly in the image block) but I didn't want to push the refactor further.

@youknowriad youknowriad requested review from gziolo and aduth Oct 27, 2018

@youknowriad

This comment has been minimized.

Copy link
Contributor Author

youknowriad commented Oct 27, 2018

Nice to see that React 16.7 is passing all of our tests. There's a lot of warnings (deprecations) though :)

@youknowriad youknowriad force-pushed the try/react-hooks branch from 2909ada to ac01e2b Oct 27, 2018

@gziolo

This comment has been minimized.

Copy link
Member

gziolo commented Oct 30, 2018

+75 −76

This is surprising (even if you subtract 2 lines added for React version upgrade).

It definitely promotes code reuse and composition similar to what HOCs offer with much easier syntax. By easier, I mean no need to create all those wrapping components which do nothing and introduce lots of confusion. I'm looking forward to the day when 16.7 stable is out 💯

@mtias

This comment has been minimized.

Copy link
Contributor

mtias commented Nov 12, 2018

I think it matches our journey between render props, higher-order wrapping functions, and support statements, and I think it would greatly simplify the expression of these (colors, alignments, etc) while providing the flexibility that "support" lacks.

@youknowriad

This comment has been minimized.

Copy link
Contributor Author

youknowriad commented Jan 7, 2019

Closing, let's revisit once hooks are officially shipped.

@youknowriad youknowriad closed this Jan 7, 2019

@youknowriad youknowriad reopened this Jan 7, 2019

@youknowriad youknowriad closed this Jan 7, 2019

@youknowriad youknowriad deleted the try/react-hooks branch Jan 7, 2019

@dsifford

This comment has been minimized.

Copy link
Contributor

dsifford commented Jan 24, 2019

Psssssssssst, @youknowriad... They've officially shipped! 😊

facebook/react#14679

@youknowriad

This comment has been minimized.

Copy link
Contributor Author

youknowriad commented Jan 24, 2019

Not quite yet, but hopefully very soon :)

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