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

Sync original #115

Merged
merged 48 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
08b6e4b
Update docs for stable Hooks
gaearon Jan 24, 2019
4d61b4a
Add Hooks to reference page
gaearon Jan 25, 2019
f510e11
Reword intro
gaearon Jan 25, 2019
9d62821
Tweak refwording
gaearon Jan 25, 2019
eaefc81
Hooks blog post draft
gaearon Jan 25, 2019
57138d7
Link to roadmap from post
gaearon Jan 25, 2019
5da272a
Add more details
gaearon Jan 29, 2019
e9081c7
Update and rename 2019-01-30-react-v16.8.0.md to 2019-02-04-react-v16…
gaearon Jan 29, 2019
d15ec6e
changelog updates
gaearon Feb 1, 2019
37d46dd
review tweaks
gaearon Feb 1, 2019
ee5fea9
Mention type defs
gaearon Feb 1, 2019
e301239
add warning for invalid hook call
gaearon Feb 1, 2019
f149987
Warning page for invalid Hook calls (#1613)
gaearon Feb 1, 2019
2fd487e
Update 2019-02-04-react-v16.8.0.md
gaearon Feb 4, 2019
ed6e711
Thanks Sophie
gaearon Feb 4, 2019
0d89570
Update 2019-02-04-react-v16.8.0.md
gaearon Feb 4, 2019
95258c0
Revert "add warning for invalid hook call"
gaearon Feb 4, 2019
e5a987b
Tweaks
gaearon Feb 4, 2019
d460a9b
Docs updates
gaearon Feb 4, 2019
f9f0d66
Point to codesandbox and usehooks as community examples
gaearon Feb 5, 2019
d97cdca
tweaks
gaearon Feb 5, 2019
601c016
testing
gaearon Feb 5, 2019
7d89762
Merge branch 'hooks-stable' of https://github.com/gaearon/reactjs.org…
Feb 5, 2019
3091413
Renamed Hooks blog/date
Feb 5, 2019
ee909e5
Udpated changelog
Feb 5, 2019
a0db3e7
Replaced inaccurate comments
Feb 5, 2019
8cae066
Added ReactTestUtils.act() to CHANGELOG
Feb 5, 2019
8fa112e
Moved blog post to Feb 6th
Feb 6, 2019
d468ba1
Merge pull request #1629 from bvaughn/hooks-stable
bvaughn Feb 6, 2019
98c1ebe
Update version number in header and versions page
nikhilem Feb 6, 2019
cea45bb
Updated 16.8 CHANGELOG wording to also mention ReactTestRenderer.act()
Feb 6, 2019
be56e22
Fix typo on hooks availability
rafaelcg Feb 6, 2019
e833dfd
Merge pull request #1632 from rafaelcg/patch-1
bvaughn Feb 6, 2019
9f4b4a9
React Native will ship hooks in its 0.59 release. (#1633)
cpojer Feb 6, 2019
845bd4a
Merge branch 'update-version-number' of https://github.com/nikhilem/r…
Feb 6, 2019
af11b29
Added link to 16.7 docs
Feb 6, 2019
2a5b6cc
Merge branch 'nikhilem-update-version-number'
Feb 6, 2019
add5007
Port external gatsby-remark-autolink-headers plugin
smikitky Feb 6, 2019
e74a555
Implement custom-id syntax on headings
smikitky Feb 6, 2019
16515ce
Replace keys when inserting style/script tags
smikitky Feb 6, 2019
f846c4d
react-testing-library now supports `act` (#1635)
Feb 6, 2019
d20be53
Document test renderer act (temporary fix)
gaearon Feb 6, 2019
8eaa701
Use 16.8.1 (#1638)
gaearon Feb 6, 2019
98c1d22
Merge pull request #1636 from smikitky/remark-autolink-custom-id
bvaughn Feb 6, 2019
b6a753b
add update everything
reactjs-translation-bot Feb 6, 2019
5364bdc
Fixing `hooks-reference`
alejandronanez Feb 7, 2019
49be9c6
Fixing hooks-custom
alejandronanez Feb 7, 2019
cbba710
Fixing: addons-test-utils
alejandronanez Feb 7, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions content/blog/2019-02-06-react-v16.8.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
---
title: "React v16.8: The One With Hooks"
author: [gaearon]
---

With React 16.8, [React Hooks](/docs/hooks-intro.html) are available in a stable release!

## What Are Hooks?

Hooks let you use state and other React features without writing a class. You can also **build your own Hooks** to share reusable stateful logic between components.

If you've never heard of Hooks before, you might find these resources interesting:

* [Introducing Hooks](/docs/hooks-intro.html) explains why we're adding Hooks to React.
* [Hooks at a Glance](/docs/hooks-overview.html) is a fast-paced overview of the built-in Hooks.
* [Building Your Own Hooks](/docs/hooks-custom.html) demonstrates code reuse with custom Hooks.
* [Making Sense of React Hooks](https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889) explores the new possibilities unlocked by Hooks.
* [useHooks.com](https://usehooks.com/) showcases community-maintained Hooks recipes and demos.

**You don't have to learn Hooks right now.** Hooks have no breaking changes, and we have no plans to remove classes from React. The [Hooks FAQ](/docs/hooks-faq.html) describes the gradual adoption strategy.

## 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 existing code using classes.

## Can I Use Hooks Today?

Yes! Starting with 16.8.0, React includes a stable implementation of React Hooks for:

* React DOM
* React DOM Server
* React Test Renderer
* React Shallow Renderer

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 support Hooks in the [0.59 release](https://github.com/react-native-community/react-native-releases/issues/79#issuecomment-457735214).**

## Tooling Support

React Hooks are now 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.

## What's Next

We described our plan for the next months in the recently published [React Roadmap](/blog/2018/11/27/react-16-roadmap.html).

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, the React community created many interesting [examples](https://codesandbox.io/react-hooks) and [recipes](https://usehooks.com) using Hooks for animations, forms, subscriptions, integrating with other libraries, and so on. 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. We can't wait to see what you'll create next!

## Testing Hooks

We have added a new API called `ReactTestUtils.act()` in this release. It ensures that the behavior in your tests matches what happens in the browser more closely. We recommend to wrap any code rendering and triggering updates to your components into `act()` calls. Testing libraries can also wrap their APIs with it (for example, [`react-testing-library`](https://github.com/kentcdodds/react-testing-library)'s `render` and `fireEvent` utilities do this).

For example, the counter example from [this page](/docs/hooks-effect.html) can be tested like this:

```js{3,20-22,29-31}
import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import Counter from './Counter';

let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('can render and update a counter', () => {
// Test first render and effect
act(() => {
ReactDOM.render(<Counter />, container);
});
const button = container.querySelector('button');
const label = container.querySelector('p');
expect(label.textContent).toBe('You clicked 0 times');
expect(document.title).toBe('You clicked 0 times');

// Test second render and effect
act(() => {
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
});
expect(label.textContent).toBe('You clicked 1 times');
expect(document.title).toBe('You clicked 1 times');
});
```

The calls to `act()` will also flush the effects inside of them.

If you need to test a custom Hook, you can do so by creating a component in your test, and using your Hook from it. Then you can test the component you wrote.

To reduce the boilerplate, we recommend using [`react-testing-library`](https://git.io/react-testing-library) which is designed to encourage writing tests that use your components as the end users do.

## Thanks

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

### React

React v16.8.0 is available on the npm registry.

To install React 16 with Yarn, run:

```bash
yarn add react@^16.8.0 react-dom@^16.8.0
```

To install React 16 with npm, run:

```bash
npm install --save react@^16.8.0 react-dom@^16.8.0
```

We also provide UMD builds of React via a CDN:

```html
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
```

Refer to the documentation for [detailed installation instructions](/docs/installation.html).

### ESLint Plugin for React Hooks

>Note
>
>As mentioned above, we strongly recommend using the `eslint-plugin-react-hooks` lint rule.
>
>If you're using Create React App, instead of manually configuring ESLint you can wait for the next version of `react-scripts` which will come out shortly and will include this rule.

Assuming you already have ESLint installed, run:

```sh
# npm
npm install eslint-plugin-react-hooks@next --save-dev

# yarn
yarn add eslint-plugin-react-hooks@next --dev
```

Then add it to your ESLint configuration:

```js
{
"plugins": [
// ...
"react-hooks"
],
"rules": {
// ...
"react-hooks/rules-of-hooks": "error"
}
}
```

## Changelog

### React

* Add [Hooks](https://reactjs.org/docs/hooks-intro.html) — a way to use state and other React features without writing a class. ([@acdlite](https://github.com/acdlite) et al. in [#13968](https://github.com/facebook/react/pull/13968))
* Improve the `useReducer` Hook lazy initialization API. ([@acdlite](https://github.com/acdlite) in [#14723](https://github.com/facebook/react/pull/14723))

### React DOM

* Bail out of rendering on identical values for `useState` and `useReducer` Hooks. ([@acdlite](https://github.com/acdlite) in [#14569](https://github.com/facebook/react/pull/14569))
* Don’t compare the first argument passed to `useEffect`/`useMemo`/`useCallback` Hooks. ([@acdlite](https://github.com/acdlite) in [#14594](https://github.com/facebook/react/pull/14594))
* Use `Object.is` algorithm for comparing `useState` and `useReducer` values. ([@Jessidhia](https://github.com/Jessidhia) in [#14752](https://github.com/facebook/react/pull/14752))
* Support synchronous thenables passed to `React.lazy()`. ([@gaearon](https://github.com/gaearon) in [#14626](https://github.com/facebook/react/pull/14626))
* Render components with Hooks twice in Strict Mode (DEV-only) to match class behavior. ([@gaearon](https://github.com/gaearon) in [#14654](https://github.com/facebook/react/pull/14654))
* Warn about mismatching Hook order in development. ([@threepointone](https://github.com/threepointone) in [#14585](https://github.com/facebook/react/pull/14585) and [@acdlite](https://github.com/acdlite) in [#14591](https://github.com/facebook/react/pull/14591))
* Effect clean-up functions must return either `undefined` or a function. All other values, including `null`, are not allowed. [@acdlite](https://github.com/acdlite) in [#14119](https://github.com/facebook/react/pull/14119)

### React Test Renderer

* Support Hooks in the shallow renderer. ([@trueadm](https://github.com/trueadm) in [#14567](https://github.com/facebook/react/pull/14567))
* Fix wrong state in `shouldComponentUpdate` in the presence of `getDerivedStateFromProps` for Shallow Renderer. ([@chenesan](https://github.com/chenesan) in [#14613](https://github.com/facebook/react/pull/14613))
* Add `ReactTestRenderer.act()` and `ReactTestUtils.act()` for batching updates so that tests more closely match real behavior. ([@threepointone](https://github.com/threepointone) in [#14744](https://github.com/facebook/react/pull/14744))

### 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 [#14661](https://github.com/facebook/react/pull/14661))
* Don't consider throwing to be a rule violation. ([@sophiebits](https://github.com/sophiebits) in [#14040](https://github.com/facebook/react/pull/14040))

## 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](/docs/faq-versioning.html), 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:

* Remove `useMutationEffect`. ([@sophiebits](https://github.com/sophiebits) in [#14336](https://github.com/facebook/react/pull/14336))
* Rename `useImperativeMethods` to `useImperativeHandle`. ([@threepointone](https://github.com/threepointone) in [#14565](https://github.com/facebook/react/pull/14565))
* Bail out of rendering on identical values for `useState` and `useReducer` Hooks. ([@acdlite](https://github.com/acdlite) in [#14569](https://github.com/facebook/react/pull/14569))
* Don’t compare the first argument passed to `useEffect`/`useMemo`/`useCallback` Hooks. ([@acdlite](https://github.com/acdlite) in [#14594](https://github.com/facebook/react/pull/14594))
* Use `Object.is` algorithm for comparing `useState` and `useReducer` values. ([@Jessidhia](https://github.com/Jessidhia) in [#14752](https://github.com/facebook/react/pull/14752))
* Render components with Hooks twice in Strict Mode (DEV-only). ([@gaearon](https://github.com/gaearon) in [#14654](https://github.com/facebook/react/pull/14654))
* Improve the `useReducer` Hook lazy initialization API. ([@acdlite](https://github.com/acdlite) in [#14723](https://github.com/facebook/react/pull/14723))
Loading