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

Update docs for stable Hooks #1593

Closed
wants to merge 21 commits into from
Closed

Conversation

gaearon
Copy link
Member

@gaearon gaearon commented Jan 24, 2019

Work in progress. Don't merge before the release.

ALSO DON'T MERGE UNTIL BLOG POST DATE IS SET TO THE REAL ONE

@reactjs-bot
Copy link

Deploy preview for reactjs ready!

Built with commit 82cd8ddfba1dab6960684a5fce27f6fb05b9173f

https://deploy-preview-1593--reactjs.netlify.com

@reactjs-bot
Copy link

Deploy preview for reactjs ready!

Built with commit b898edfc8b1ca941a31f354eea3601ca5c9901fa

https://deploy-preview-1593--reactjs.netlify.com

@reactjs-bot
Copy link

reactjs-bot commented Jan 24, 2019

Deploy preview for reactjs ready!

Built with commit 601c016

https://deploy-preview-1593--reactjs.netlify.com

* [Do I need to rewrite all my class components?](#do-i-need-to-rewrite-all-my-class-components)
* [What can I do with Hooks that I couldn't with classes?](#what-can-i-do-with-hooks-that-i-couldnt-with-classes)
* [How much of my React knowledge stays relevant?](#how-much-of-my-react-knowledge-stays-relevant)
* [Should I use Hooks, classes, or a mix of both?](#should-i-use-hooks-classes-or-a-mix-of-both)
* [Do Hooks cover all use cases for classes?](#do-hooks-cover-all-use-cases-for-classes)
Copy link

@Avi98 Avi98 Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gaearon, I know its still in progress but still I just wanted to highlight that in the answer of the above question some things need to be changed as I have mentioned in this issue that react devtools and flow now support hooks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already edited that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry just saw that. I'll close that issue

Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

content/docs/hooks-custom.md Outdated Show resolved Hide resolved
@gaearon
Copy link
Member Author

gaearon commented Jan 25, 2019

Pushed blog post draft.

(Note: DON'T MERGE UNTIL BLOG POST DATE IS SET TO THE REAL ONE)

Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post looks great


## Tooling Support

React Hooks are now fully supported by React DevTools. They are also supported in the latest Flow and TypeScript definitions for React. We recommend using a new [lint rule](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce best practices with Hooks. It will soon be included into Create React App by default.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:
We strongly recommend enabling a new [lint rule called `eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce best practices with Hooks.

CRA soon? Why not now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to make it a release blocker because we also wanted to get facebook/create-react-app#6219 in and possibly facebook/react#14636. But maybe we should just split those.


Note that React Hooks don't cover *all* use cases for classes yet but they're [very close](/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes). Currently, only `getSnapshotBeforeUpdate()` and `componentDidCatch()` methods don't have equivalent Hooks APIs, and these lifecycles are relatively uncommon. If you want, you should be able to use Hooks in most of the new code you're writing.

Even while Hooks were in alpha, we saw many [interesting examples](https://medium.com/@drcmda/hooks-in-react-spring-a-tutorial-c6c436ad7ee4) of custom Hooks for animations, forms, subscriptions, integrating with other libraries, and so on. Our goal is to empower the React community to write components and Hooks that deliver great user and developer experience. We can't wait to see what you'll create next!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Our goal…" is a little fluffy. I might write:

We're excited about Hooks because they make code reuse easier, helping you write your components in a simpler way and make great user experiences.

(Take or leave, I don't mind the current one either.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was trying to say our job is to focus on the primitives


We'd like to thank everybody who commented on the [Hooks RFC](https://github.com/reactjs/rfcs/pull/68) for sharing their feedback. We've read all of your comments and made some adjustments to the final API based on them.

## Installation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add lint rule to this section?

### ESLint Plugin: React Hooks

* Initial [release](https://www.npmjs.com/package/eslint-plugin-react-hooks). ([@calebmer](https://github.com/calebmer) in [#13968](https://github.com/facebook/react/pull/13968))
* Fix reporting after encountering a loop. ([@calebmer](https://github.com/calebmer) and [@Yurickh](https://github.com/Yurickh) in [#13968](https://github.com/facebook/react/pull/13968))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about facebook/react#14040 🙃

@gaearon
Copy link
Member Author

gaearon commented Jan 29, 2019

I added more details to changelog. Practically these PRs can be considered part of Hooks but I figured alpha users might appreciate calling out the changes.

Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include the breaking changes in Hooks since our initial alpha somewhere prominent in the post?


## Tooling Support

React Hooks are now fully supported by React DevTools. They are also supported in the latest Flow and TypeScript definitions for React. We strongly recommend enabling a new [lint rule called `eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce best practices with Hooks. It will soon be included into Create React App by default.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take the opportunity to ask a question if you don't mind. I noticed that hooks' values aren't easily editable like class properties in the DevTools, which was a surprise for me.

react

Although they could be considered "fully-supported" in the sense of what's possible to do in regards to implementation details, I'd think the passage also implies feature-parity with the way people are used to interact with class components.

The way the sentence is structured implies a solved problem. Is it the case? Or will hooks' values also be as flexible in the future?


React 16.8.0 is the first **stable** release supporting Hooks.

Over the past few months, we have also provided several **alpha** versions for community feedback: `16.7.0-alpha.0`, `16.7.0-alpha.1`, `16.7.0-alpha.2`, and `16.8.0-alpha.0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to mention 16.7-.1? IIRC it didn't have hooks (oops)


**We don't recommend depending on alphas in production code.** We publish them so we can make breaking changes in response to community feedback before the API is stable.

Here is a list of all breaking changes to Hooks between the first alpha and the stable release:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remind people no "real" breaking changes? Maybe:

Like all minor releases, 16.8.0 contains no breaking changes relative to previous 16.x releases (16.0.0 through 16.7.0). For devs currently using an alpha release of React with Hooks, please note this list of the breaking changes we've made since the first alpha.

OR ALTERNATIVELY:

Can we move this to the very end of the post (after ## Changelog) and do like:

Thanks

...

Hooks Changelog Since Alpha Versions

The above changelog contains all notable changes since our last stable release (16.7.0). [As with all our minor releases](versioning policy), none of the changes break backwards compatibility.

If you're currently using Hooks from an alpha build of React, note that this release does contain some small breaking changes to Hooks. We don't recommend depending on alphas in production code. We publish them so we can make changes in response to community feedback before the API is stable.

Here are all breaking changes to Hooks that have been made since the first alpha release:

I think I like that better. 70% of readers probably won't need the section so it's better to move it later IMO.


### ESLint Plugin for React Hooks

>Note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+As mentioned above, we strongly recommend using the `eslint-plugin-react-hooks` lint rule.
+


## No Big Rewrites

We don't recommend rewriting your existing applications to use Hooks overnight. Instead, try using Hooks in some of the new components, and let us know what you think. Code using Hooks will work [side by side](/docs/hooks-intro.html#gradual-adoption-strategy) with your existing code using classes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "with your existing code" -> "with existing code" for consistency with the first half of the sentence (and brevity)


Note that **to enable Hooks, all React packages need to be 16.8.0 or higher**. Hooks won't work if you forget to update, for example, React DOM.

**React Native will fully support Hooks in its next stable release.**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we include a version number?


**React Native will fully support Hooks in its next stable release.**

The final TypeScript and Flow definitions are currently in review and will be available soon.
Copy link

@Jessidhia Jessidhia Feb 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was actually accidentally merged early and is already available in @types/react@16.8.1 😅

Fortunately the only part that is not compatible for current alpha users is the 3rd argument to useReduce.

Copy link

@Jessidhia Jessidhia Feb 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, the DefinitelyTyped publish bot does an auto-publish, with a patch version bump, every time a PR is merged. There were 2 PRs to update the API so the first publish to have both was 16.8.1.


>Note
>
>React doesn’t use the `state = initialState` argument convention popularized by Redux. The initial value sometimes needs to depend on props and so is specified from the Hook call instead. If you feel strongly about this, you can write `state = initialState` both in the reducer and inside the `useReducer` destructuring assignment, but it's not encouraged.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I found this very hard to parse due to my unfamiliarity. Maybe…

To specify initial state, React doesn't use the default arguments pattern function reducer(action, state = {count: 0}) popularized by Redux.


you can write state = initialState both in the reducer and inside the useReducer destructuring assignment

can you not put it only in the reducer and let the initial state default to undefined? why?

@@ -322,6 +323,22 @@ function ScrollView({row}) {

This might look strange at first, but an update during rendering is exactly what `getDerivedStateFromProps` has always been like conceptually.

### Is there something like forceUpdate?

Both `useState` and `useReducer` Hooks [bail out of updates](/docs/hooks-reference.html#bailing-out-of-a-state-update) if the next value is the same as the previous one. Mutating state in place and calling `setState` will not cause a re-render.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"bail out" feels jargony – how do you feel about "skip rerendering"?

@@ -28,7 +28,7 @@ By following this rule, you ensure that all stateful logic in a component is cle
We released an ESLint plugin called [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) that enforces these two rules. You can add this plugin to your project if you'd like to try it:

```bash
npm install eslint-plugin-react-hooks@next
npm install eslint-plugin-react-hooks
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a plugin that automatically provides hooks with the right arguments? Such as useMemo and useCallback. It's also mentioned in the docs:

Note

The array of inputs is not passed as arguments to the callback. Conceptually, though, that’s what they represent: every value referenced inside the callback should also appear in the inputs array. In the future, a sufficiently advanced compiler could create this array automatically.

Check out a plugin of mine (still experimental): https://github.com/DAB0mB/babel-plugin-react-persist Would be nice to add it to the docs rather than people just searching around, or even worse, not using hooks when necessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a part of facebook/react#14636

@bvaughn
Copy link
Contributor

bvaughn commented Feb 5, 2019

Dan's gone for the day and we don't have write access to his repo so I've forked and opened a new PR #1629

@bvaughn bvaughn closed this Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet