Skip to content

Commit 08b6e4b

Browse files
committed
Update docs for stable Hooks
1 parent 024b5b6 commit 08b6e4b

9 files changed

+62
-34
lines changed

content/docs/hooks-custom.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ next: hooks-reference.html
66
prev: hooks-rules.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
Building your own Hooks lets you extract component logic into reusable functions.
1212

1313
When we were learning about [using the Effect Hook](/docs/hooks-effect.html#example-using-hooks-1), we saw this component from a chat application that displays a message indicating whether a friend is online or offline:
1414

1515
```js{4-15}
16-
import { useState, useEffect } from 'react';
16+
import React, { useState, useEffect } from 'react';
1717
1818
function FriendStatus(props) {
1919
const [isOnline, setIsOnline] = useState(null);
@@ -39,7 +39,7 @@ function FriendStatus(props) {
3939
Now let's say that our chat application also has a contact list, and we want to render names of online users with a green color. We could copy and paste similar logic above into our `FriendListItem` component but it wouldn't be ideal:
4040

4141
```js{4-15}
42-
import { useState, useEffect } from 'react';
42+
import React, { useState, useEffect } from 'react';
4343
4444
function FriendListItem(props) {
4545
const [isOnline, setIsOnline] = useState(null);
@@ -74,7 +74,7 @@ When we want to share logic between two JavaScript functions, we extract it to a
7474
**A custom Hook is a JavaScript function whose name starts with "`use`" and that may call other Hooks.** For example, `useFriendStatus` below is our first custom Hook:
7575

7676
```js{3}
77-
import { useState, useEffect } from 'react';
77+
import React, { useState, useEffect } from 'react';
7878
7979
function useFriendStatus(friendID) {
8080
const [isOnline, setIsOnline] = useState(null);

content/docs/hooks-effect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ next: hooks-rules.html
66
prev: hooks-intro.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
The *Effect Hook* lets you perform side effects in function components:
1212

1313
```js{1,6-10}
14-
import { useState, useEffect } from 'react';
14+
import React, { useState, useEffect } from 'react';
1515
1616
function Example() {
1717
const [count, setCount] = useState(0);
@@ -94,7 +94,7 @@ Now let's see how we can do the same with the `useEffect` Hook.
9494
We've already seen this example at the top of this page, but let's take a closer look at it:
9595

9696
```js{1,6-8}
97-
import { useState, useEffect } from 'react';
97+
import React, { useState, useEffect } from 'react';
9898
9999
function Example() {
100100
const [count, setCount] = useState(0);
@@ -199,7 +199,7 @@ Let's see how we could write this component with Hooks.
199199
You might be thinking that we'd need a separate effect to perform the cleanup. But code for adding and removing a subscription is so tightly related that `useEffect` is designed to keep it together. If your effect returns a function, React will run it when it is time to clean up:
200200
201201
```js{10-16}
202-
import { useState, useEffect } from 'react';
202+
import React, { useState, useEffect } from 'react';
203203

204204
function FriendStatus(props) {
205205
const [isOnline, setIsOnline] = useState(null);

content/docs/hooks-faq.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ permalink: docs/hooks-faq.html
55
prev: hooks-reference.html
66
---
77

8-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
8+
*Hooks* let you use state and other React features without writing a class.
99

1010
This page answers some of the frequently asked questions about [Hooks](/docs/hooks-overview.html).
1111

@@ -19,7 +19,9 @@ This page answers some of the frequently asked questions about [Hooks](/docs/hoo
1919
-->
2020

2121
* **[Adoption Strategy](#adoption-strategy)**
22+
* [Which versions of React include Hooks?](#which-versions-of-react-include-hooks)
2223
* [Do I need to rewrite all my class components?](#do-i-need-to-rewrite-all-my-class-components)
24+
* [What can I do with Hooks that I couldn't with classes?](#what-can-i-do-with-hooks-that-i-couldnt-with-classes)
2325
* [How much of my React knowledge stays relevant?](#how-much-of-my-react-knowledge-stays-relevant)
2426
* [Should I use Hooks, classes, or a mix of both?](#should-i-use-hooks-classes-or-a-mix-of-both)
2527
* [Do Hooks cover all use cases for classes?](#do-hooks-cover-all-use-cases-for-classes)
@@ -51,10 +53,27 @@ This page answers some of the frequently asked questions about [Hooks](/docs/hoo
5153

5254
## Adoption Strategy
5355

56+
### Which versions of React include Hooks?
57+
58+
Starting with 16.8.0, React includes a stable implementation of React Hooks for:
59+
60+
* React DOM
61+
* React DOM Server
62+
* React Test Renderer
63+
* React Shallow Renderer
64+
65+
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.
66+
67+
React Native will fully support Hooks in its next stable release.
68+
5469
### Do I need to rewrite all my class components?
5570

5671
No. There are [no plans](/docs/hooks-intro.html#gradual-adoption-strategy) to remove classes from React -- we all need to keep shipping products and can't afford rewrites. We recommend trying Hooks in new code.
5772

73+
### What can I do with Hooks that I couldn't with classes?
74+
75+
Hooks offer a powerful and expressive new way to reuse functionality between components. ["Building Your Own Hooks"](/docs/hooks-custom.html) provides a glimpse of what's possible. [This article](https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889) by a React core team member dives deeper into the new capabilities unlocked by Hooks.
76+
5877
### How much of my React knowledge stays relevant?
5978

6079
Hooks are a more direct way to use the React features you already know -- such as state, lifecycle, context, and refs. They don't fundamentally change how React works, and your knowledge of components, props, and top-down data flow is just as relevant.
@@ -71,7 +90,7 @@ You can't use Hooks *inside* of a class component, but you can definitely mix cl
7190

7291
Our goal is for Hooks to cover all use cases for classes as soon as possible. There are no Hook equivalents to the uncommon `getSnapshotBeforeUpdate` and `componentDidCatch` lifecycles yet, but we plan to add them soon.
7392

74-
It is a very early time for Hooks, so some integrations like DevTools support or Flow/TypeScript typings may not be ready yet. Some third-party libraries might also not be compatible with Hooks at the moment.
93+
It is an early time for Hooks, and some third-party libraries might not be compatible with Hooks at the moment.
7594

7695
### Do Hooks replace render props and higher-order components?
7796

@@ -85,7 +104,7 @@ In the future, new versions of these libraries might also export custom Hooks su
85104

86105
### Do Hooks work with static typing?
87106

88-
Hooks were designed with static typing in mind. Because they're functions, they are easier to type correctly than patterns like higher-order components. We have reached out both to Flow and TypeScript teams in advance, and they plan to include definitions for React Hooks in the future.
107+
Hooks were designed with static typing in mind. Because they're functions, they are easier to type correctly than patterns like higher-order components. The latest Flow and TypeScript React definitions include support for React Hooks.
89108

90109
Importantly, custom Hooks give you the power to constrain React API if you'd like to type them more strictly in some way. React gives you the primitives, but you can combine them in different ways than what we provide out of the box.
91110

content/docs/hooks-intro.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ permalink: docs/hooks-intro.html
55
next: hooks-overview.html
66
---
77

8-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
8+
*Hooks* let you use state and other React features without writing a class.
99

1010
```js{4,5}
11-
import { useState } from 'react';
11+
import React, { useState } from 'react';
1212
1313
function Example() {
1414
// Declare a new state variable, which we'll call "count"
@@ -29,6 +29,10 @@ This new function `useState` is the first "Hook" we'll learn about, but this exa
2929

3030
**You can start learning Hooks [on the next page](/docs/hooks-overview.html).** On this page, we'll continue by explaining why we're adding Hooks to React and how they can help you write great applications.
3131

32+
>Note
33+
>
34+
>React 16.8.0 is the first release to support Hooks. When upgrading, don't forget to update all packages, including React DOM. React Native will support Hooks in the next stable release.
35+
3236
## Video Introduction
3337

3438
At React Conf 2018, Sophie Alpert and Dan Abramov introduced Hooks, followed by Ryan Florence demonstrating how to refactor an application to use them. Watch the video here:
@@ -99,6 +103,10 @@ Finally, there is no rush to migrate to Hooks. We recommend avoiding any "big re
99103

100104
We intend for Hooks to cover all existing use cases for classes, but **we will keep supporting class components for the foreseeable future.** At Facebook, we have tens of thousands of components written as classes, and we have absolutely no plans to rewrite them. Instead, we are starting to use Hooks in the new code side by side with classes.
101105

106+
## Frequently Asked Questions
107+
108+
We've prepared a [Hooks FAQ page](/docs/hooks-faq.html) that answers the most common questions about Hooks.
109+
102110
## Next Steps
103111

104112
By the end of this page, you should have a rough idea of what problems Hooks are solving, but many details are probably unclear. Don't worry! **Let's now go to [the next page](/docs/hooks-overview.html) where we start learning about Hooks by example.**

content/docs/hooks-overview.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ next: hooks-state.html
66
prev: hooks-intro.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
Hooks are [backwards-compatible](/docs/hooks-intro.html#no-breaking-changes). This page provides an overview of Hooks for experienced React users. This is a fast-paced overview. If you get confused, look for a yellow box like this:
1212

@@ -21,7 +21,7 @@ Hooks are [backwards-compatible](/docs/hooks-intro.html#no-breaking-changes). Th
2121
This example renders a counter. When you click the button, it increments the value:
2222

2323
```js{1,4,5}
24-
import { useState } from 'react';
24+
import React, { useState } from 'react';
2525
2626
function Example() {
2727
// Declare a new state variable, which we'll call "count"
@@ -77,7 +77,7 @@ The Effect Hook, `useEffect`, adds the ability to perform side effects from a fu
7777
For example, this component sets the document title after React updates the DOM:
7878

7979
```js{1,6-10}
80-
import { useState, useEffect } from 'react';
80+
import React, { useState, useEffect } from 'react';
8181
8282
function Example() {
8383
const [count, setCount] = useState(0);
@@ -104,7 +104,7 @@ When you call `useEffect`, you're telling React to run your "effect" function af
104104
Effects may also optionally specify how to "clean up" after them by returning a function. For example, this component uses an effect to subscribe to a friend's online status, and cleans up by unsubscribing from it:
105105

106106
```js{10-16}
107-
import { useState, useEffect } from 'react';
107+
import React, { useState, useEffect } from 'react';
108108
109109
function FriendStatus(props) {
110110
const [isOnline, setIsOnline] = useState(null);
@@ -181,7 +181,7 @@ Earlier on this page, we introduced a `FriendStatus` component that calls the `u
181181
First, we'll extract this logic into a custom Hook called `useFriendStatus`:
182182

183183
```js{3}
184-
import { useState, useEffect } from 'react';
184+
import React, { useState, useEffect } from 'react';
185185
186186
function useFriendStatus(friendID) {
187187
const [isOnline, setIsOnline] = useState(null);

content/docs/hooks-reference.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ prev: hooks-custom.html
66
next: hooks-faq.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
This page describes the APIs for the built-in Hooks in React.
1212

@@ -367,15 +367,15 @@ useDebugValue(value)
367367

368368
`useDebugValue` can be used to display a label for custom hooks in React DevTools.
369369

370-
For example, consider the `useFriendStatus` custom hook described in ["Building Your Own Hooks"](/docs/hooks-custom.html):
370+
For example, consider the `useFriendStatus` custom Hook described in ["Building Your Own Hooks"](/docs/hooks-custom.html):
371371

372372
```js{6-8}
373373
function useFriendStatus(friendID) {
374374
const [isOnline, setIsOnline] = useState(null);
375375
376376
// ...
377377
378-
// Show a label in DevTools next to this hook
378+
// Show a label in DevTools next to this Hook
379379
// e.g. "FriendStatus: Online"
380380
useDebugValue(isOnline ? 'Online' : 'Offline');
381381
@@ -385,15 +385,16 @@ function useFriendStatus(friendID) {
385385

386386
> Tip
387387
>
388-
> We don't recommend adding debug values to every custom hook. It's most valuable for custom hooks that are part of shared libraries.
388+
> We don't recommend adding debug values to every custom Hook. It's most valuable for custom Hooks that are part of shared libraries.
389389
390390
#### Defer formatting debug values
391391

392-
In some cases formatting a value for display might be an expensive operation. It's also unnecessary unless a hook is actually inspected.
392+
In some cases formatting a value for display might be an expensive operation. It's also unnecessary unless a Hook is actually inspected.
393393

394-
For this reason `useDebugValue` accepts a formatting function as an optional second parameter. This function is only called if the hooks is inspected. It receives the debug value as a parameter and should return a formatted display value.
394+
For this reason `useDebugValue` accepts a formatting function as an optional second parameter. This function is only called if the Hooks are inspected. It receives the debug value as a parameter and should return a formatted display value.
395+
396+
For example a custom Hook that returned a `Date` value could avoid calling the `toDateString` function unnecessarily by passing the following formatter:
395397

396-
For example a custom hook that returned a `Date` value could avoid calling the `toDateString` function unnecessarily by passing the following formatter:
397398
```js
398399
useDebugValue(date, date => date.toDateString());
399400
```

content/docs/hooks-rules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ next: hooks-custom.html
66
prev: hooks-effect.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce these rules automatically:
1212

@@ -28,7 +28,7 @@ By following this rule, you ensure that all stateful logic in a component is cle
2828
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:
2929

3030
```bash
31-
npm install eslint-plugin-react-hooks@next
31+
npm install eslint-plugin-react-hooks
3232
```
3333

3434
```js

content/docs/hooks-state.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ next: hooks-effect.html
66
prev: hooks-overview.html
77
---
88

9-
*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1.
9+
*Hooks* let you use state and other React features without writing a class.
1010

1111
The [previous page](/docs/hooks-intro.html) introduced Hooks with this example:
1212

1313
```js{4-5}
14-
import { useState } from 'react';
14+
import React, { useState } from 'react';
1515
1616
function Example() {
1717
// Declare a new state variable, which we'll call "count"
@@ -91,7 +91,7 @@ Hooks **don't** work inside classes. But you can use them instead of writing cla
9191
Our new example starts by importing the `useState` Hook from React:
9292

9393
```js{1}
94-
import { useState } from 'react';
94+
import React, { useState } from 'react';
9595
9696
function Example() {
9797
// ...
@@ -123,7 +123,7 @@ class Example extends React.Component {
123123
In a function component, we have no `this`, so we can't assign or read `this.state`. Instead, we call the `useState` Hook directly inside our component:
124124

125125
```js{4,5}
126-
import { useState } from 'react';
126+
import React, { useState } from 'react';
127127
128128
function Example() {
129129
// Declare a new state variable, which we'll call "count"
@@ -139,7 +139,7 @@ function Example() {
139139
Now that we know what the `useState` Hook does, our example should make more sense:
140140

141141
```js{4,5}
142-
import { useState } from 'react';
142+
import React, { useState } from 'react';
143143
144144
function Example() {
145145
// Declare a new state variable, which we'll call "count"
@@ -196,7 +196,7 @@ Let's now **recap what we learned line by line** and check our understanding.
196196
But if GitHub got away with it for years we can cheat.
197197
-->
198198
```js{1,4,9}
199-
1: import { useState } from 'react';
199+
1: import React, { useState } from 'react';
200200
2:
201201
3: function Example() {
202202
4: const [count, setCount] = useState(0);

content/docs/nav.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
title: JS Environment Requirements
105105
- id: glossary
106106
title: Glossary
107-
- title: Hooks (Preview)
107+
- title: Hooks (New)
108108
isOrdered: true
109109
items:
110110
- id: hooks-intro

0 commit comments

Comments
 (0)