Skip to content
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 from

Conversation

youknowriad
Copy link
Contributor

@youknowriad 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
Copy link
Contributor Author

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

@gziolo
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 💯

@gziolo gziolo added the [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible label Oct 30, 2018
@mtias
Copy link
Member

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
Copy link
Contributor Author

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 January 7, 2019 14:34
@dsifford
Copy link
Contributor

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

facebook/react#14679

@youknowriad
Copy link
Contributor Author

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
Labels
[Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants