Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into state-and-lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnilssondev committed Aug 14, 2019
2 parents 735e364 + eb0e0cd commit 6633c1e
Show file tree
Hide file tree
Showing 31 changed files with 1,204 additions and 54 deletions.
9 changes: 9 additions & 0 deletions content/blog/2018-03-27-update-on-async-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ These lifecycle methods have often been misunderstood and subtly misused; furthe

We maintain over 50,000 React components at Facebook, and we don't plan to rewrite them all immediately. We understand that migrations take time. We will take the gradual migration path along with everyone in the React community.

If you don't have the time to migrate or test these components, we recommend running a ["codemod"](https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb) script that renames them automatically:

```bash
cd your_project
npx react-codemod rename-unsafe-lifecycles
```

Learn more about this codemod on the [16.9.0 release post.](https://reactjs.org/blog/2019/08/08/react-v16.9.0.html#renaming-unsafe-lifecycle-methods)

---

## Migrating from Legacy Lifecycles {#migrating-from-legacy-lifecycles}
Expand Down
5 changes: 5 additions & 0 deletions content/blog/2018-11-27-react-16-roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ author: [gaearon]
---

You might have heard about features like "Hooks", "Suspense", and "Concurrent Rendering" in the previous blog posts and talks. In this post, we'll look at how they fit together and the expected timeline for their availability in a stable release of React.

> An Update from August, 2019
>
> You can find an update to this roadmap in the [React 16.9 release blog post](/blog/2019/08/08/react-v16.9.0.html#an-update-to-the-roadmap).

## tl;dr {#tldr}

Expand Down
4 changes: 2 additions & 2 deletions content/blog/2019-02-06-react-v16.8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Even while Hooks were in alpha, the React community created many interesting [ex

## Testing Hooks {#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).
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://testing-library.com/react)'s `render` and `fireEvent` utilities do this).

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

Expand Down Expand Up @@ -95,7 +95,7 @@ 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.
To reduce the boilerplate, we recommend using [`react-testing-library`](https://testing-library.com/react) which is designed to encourage writing tests that use your components as the end users do.

## Thanks {#thanks}

Expand Down
241 changes: 241 additions & 0 deletions content/blog/2019-08-08-react-v16.9.0.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions content/community/articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ permalink: community/articles.html
- [Timeline for Learning React](https://daveceddia.com/timeline-for-learning-react/) - Dave Ceddia's recommended timeline for learning React and the React ecosystem.
- [Simple React Development in 2017](https://hackernoon.com/simple-react-development-in-2017-113bd563691f) - Joshua Comeau's guide to showcase how easy it can be to start modern React development.
- [Visual Guide to State in React](https://daveceddia.com/visual-guide-to-state-in-react/) - Dave Ceddia's visual guide to React state.
- [The Hands-On Guide to Learning React Hooks](https://www.telerik.com/kendo-react-ui/react-hooks-guide/)
5 changes: 5 additions & 0 deletions content/community/conferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Do you know of a local React.js conference? Add it here! (Please keep the list c

## Upcoming Conferences {#upcoming-conferences}

### React Conf Iran 2019 {#react-conf-iran-2019}
August 29, 2019. Tehran, Iran.

[Website](https://reactconf.ir/) - [Twitter](https://twitter.com/reactconf_ir) - [Instagram](https://www.instagram.com/reactconf/)

### React Rally 2019 {#react-rally-2019}
August 22-23, 2019. Salt Lake City, USA.

Expand Down
6 changes: 6 additions & 0 deletions content/community/meetups.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Do you have a local React.js meetup? Add it here! (Please keep the list alphabet
## Brazil {#brazil}
* [Belo Horizonte](https://www.meetup.com/reactbh/)
* [Curitiba](https://www.meetup.com/pt-br/ReactJS-CWB/)
* [Joinville](https://www.meetup.com/pt-BR/React-Joinville/)
* [Rio de Janeiro](https://www.meetup.com/pt-BR/React-Rio-de-Janeiro/)
* [São Paulo](https://www.meetup.com/pt-BR/ReactJS-SP/)

Expand Down Expand Up @@ -107,8 +108,12 @@ Do you have a local React.js meetup? Add it here! (Please keep the list alphabet
## Scotland (UK) {#scotland-uk}
* [Edinburgh](https://www.meetup.com/React-Scotland/)

## Singapore {#singapore}
* [Singapore - React Knowledgeable](https://reactknowledgeable.org/)

## Spain {#spain}
* [Barcelona](https://www.meetup.com/ReactJS-Barcelona/)
* [Canarias](https://www.meetup.com/React-Canarias/)

## Sweden {#sweden}
* [Goteborg](https://www.meetup.com/ReactJS-Goteborg/)
Expand All @@ -130,6 +135,7 @@ Do you have a local React.js meetup? Add it here! (Please keep the list alphabet
* [Leesburg, VA - ReactJS](https://www.meetup.com/React-NOVA/)
* [Los Angeles, CA - ReactJS](https://www.meetup.com/socal-react/)
* [Los Angeles, CA - React Native](https://www.meetup.com/React-Native-Los-Angeles/)
* [Miami, FL - ReactJS](https://www.meetup.com/React-Miami/)
* [Nashville, TN - ReactJS](https://www.meetup.com/NashReact-Meetup/)
* [New York, NY - ReactJS](https://www.meetup.com/NYC-Javascript-React-Group/)
* [New York, NY - React Ladies](https://www.meetup.com/React-Ladies/)
Expand Down
2 changes: 0 additions & 2 deletions content/community/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
title: Model Management
- id: data-fetching
title: Data Fetching
- id: testing
title: Testing
- id: ui-components
title: UI Components
- id: misc
Expand Down
13 changes: 0 additions & 13 deletions content/community/tools-testing.md

This file was deleted.

3 changes: 1 addition & 2 deletions content/community/videos.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ Facebook engineers Bill Fisher and Jing Chen talk about Flux and React at Forwar
### Going Big with React {#going-big-with-react}

Areeb Malik investigates how React performs in a high stress situation, and how it helped his team build safe code on a massive scale - (2014 - 0h31m).
[![going big with React](https://i.vimeocdn.com/video/481670116_650.jpg)]

<iframe title="Areeb Malik : Going big with React" width="650" height="366" src="https://www.youtube-nocookie.com/embed/9qcBlN6-qwY" frameborder="0" allowfullscreen></iframe>

### Rethinking Best Practices {#rethinking-best-practices}

Expand Down
6 changes: 4 additions & 2 deletions content/docs/addons-test-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ it('can render and update a counter', () => {
});
```

Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a helper like [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) to reduce the boilerplate code.
- Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a library like [React Testing Library](https://testing-library.com/react) to reduce the boilerplate code.

- The [`recipes`](/docs/recipes.html) document contains more details on how `act()` behaves, with examples and usage.

* * *

Expand All @@ -139,7 +141,7 @@ Pass a mocked component module to this method to augment it with useful methods

> Note:
>
> `mockComponent()` is a legacy API. We recommend using [shallow rendering](/docs/shallow-renderer.html) or [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) instead.
> `mockComponent()` is a legacy API. We recommend using [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) instead.
* * *

Expand Down
3 changes: 2 additions & 1 deletion content/docs/code-splitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ permalink: docs/code-splitting.html
## Bundling {#bundling}

Most React apps will have their files "bundled" using tools like
[Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/).
[Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/) or
[Browserify](http://browserify.org/).
Bundling is the process of following imported files and merging them into a
single file: a "bundle". This bundle can then be included on a webpage to load
an entire app at once.
Expand Down
4 changes: 2 additions & 2 deletions content/docs/error-boundaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class ErrorBoundary extends React.Component {
return { hasError: true };
}
componentDidCatch(error, info) {
componentDidCatch(error, errorInfo) {
// You can also log the error to an error reporting service
logErrorToMyService(error, info);
logErrorToMyService(error, errorInfo);
}
render() {
Expand Down
2 changes: 1 addition & 1 deletion content/docs/faq-styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ _Note that this functionality is not a part of React, but provided by third-part

### Can I do animations in React? {#can-i-do-animations-in-react}

React can be used to power animations. See [React Transition Group](https://reactcommunity.org/react-transition-group/) and [React Motion](https://github.com/chenglou/react-motion), for example.
React can be used to power animations. See [React Transition Group](https://reactcommunity.org/react-transition-group/) and [React Motion](https://github.com/chenglou/react-motion) or [React Spring](https://github.com/react-spring/react-spring), for example.
1 change: 1 addition & 0 deletions content/docs/hooks-effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function Example() {
useEffect(() => {
document.title = `You clicked ${count} times`;
});
}
```

We declare the `count` state variable, and then we tell React we need to use an effect. We pass a function to the `useEffect` Hook. This function we pass *is* our effect. Inside our effect, we set the document title using the `document.title` browser API. We can read the latest `count` inside the effect because it's in the scope of our function. When React renders our component, it will remember the effect we used, and then run our effect after updating the DOM. This happens for every render, including the first one.
Expand Down
14 changes: 11 additions & 3 deletions content/docs/hooks-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ Often, render props and higher-order components render only a single child. We t

You can continue to use the exact same APIs as you always have; they'll continue to work.

In the future, new versions of these libraries might also export custom Hooks such as `useRedux()` or `useRouter()` that let you use the same features without needing wrapper components.
React Redux since v7.1.0 [supports Hooks API](https://react-redux.js.org/api/hooks) and exposes hooks like `useDispatch` or `useSelector`.

Libraries like React Router might support hooks in the future.

### Do Hooks work with static typing? {#do-hooks-work-with-static-typing}

Expand All @@ -118,6 +120,10 @@ Importantly, custom Hooks give you the power to constrain React API if you'd lik

From React's point of view, a component using Hooks is just a regular component. If your testing solution doesn't rely on React internals, testing components with Hooks shouldn't be different from how you normally test components.

>Note
>
>[Testing Recipes](/docs/testing-recipes.html) include many examples that you can copy and paste.
For example, let's say we have this counter component:

```js
Expand Down Expand Up @@ -180,7 +186,9 @@ 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.
To reduce the boilerplate, we recommend using [React Testing Library](https://testing-library.com/react) which is designed to encourage writing tests that use your components as the end users do.

For more information, check out [Testing Recipes](/docs/testing-recipes.html).

### What exactly do the [lint rules](https://www.npmjs.com/package/eslint-plugin-react-hooks) enforce? {#what-exactly-do-the-lint-rules-enforce}

Expand Down Expand Up @@ -563,7 +571,7 @@ Depending on your use case, there are a few more options described below.

>Note
>
>We provide the [`exhaustive-deps`](https://github.com/facebook/react/issues/14920) ESLint rule as a part of the [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks#installation) package. It help you find components that don't handle updates consistently.
>We provide the [`exhaustive-deps`](https://github.com/facebook/react/issues/14920) ESLint rule as a part of the [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks#installation) package. It helps you find components that don't handle updates consistently.
Let's see why this matters.

Expand Down
10 changes: 10 additions & 0 deletions content/docs/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
title: Optimizing Performance
- id: portals
title: Portals
- id: profiler
title: Profiler
- id: react-without-es6
title: React Without ES6
- id: react-without-jsx
Expand Down Expand Up @@ -123,6 +125,14 @@
title: Hooks API Reference
- id: hooks-faq
title: Hooks FAQ
- title: Testing
items:
- id: testing
title: Testing Overview
- id: testing-recipes
title: Testing Recipes
- id: testing-environments
title: Testing Environments
- title: Contributing
items:
- id: how-to-contribute
Expand Down
119 changes: 119 additions & 0 deletions content/docs/reference-profiler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
id: profiler
title: Profiler API
layout: docs
category: Reference
permalink: docs/profiler.html
---

The `Profiler` measures how often a React application renders and what the "cost" of rendering is.
Its purpose is to help identify parts of an application that are slow and may benefit from [optimizations such as memoization](https://reactjs.org/docs/hooks-faq.html#how-to-memoize-calculations).

> Note:
>
> Profiling adds some additional overhead, so **it is disabled in [the production build](https://reactjs.org/docs/optimizing-performance.html#use-the-production-build)**.
>
> To opt into production profiling, React provides a special production build with profiling enabled.
> Read more about how to use this build at [fb.me/react-profiling](https://fb.me/react-profiling)
## Usage

A `Profiler` can be added anywhere in a React tree to measure the cost of rendering that part of the tree.
It requires two props: an `id` (string) and an `onRender` callback (function) which React calls any time a component within the tree "commits" an update.

For example, to profile a `Navigation` component and its descendants:

```js{3}
render(
<App>
<Profiler id="Navigation" onRender={callback}>
<Navigation {...props} />
</Profiler>
<Main {...props} />
</App>
);
```

Multiple `Profiler` components can be used to measure different parts of an application:
```js{3,6}
render(
<App>
<Profiler id="Navigation" onRender={callback}>
<Navigation {...props} />
</Profiler>
<Profiler id="Main" onRender={callback}>
<Main {...props} />
</Profiler>
</App>
);
```

`Profiler` components can also be nested to measure different components within the same subtree:
```js{2,6,8}
render(
<App>
<Profiler id="Panel" onRender={callback}>
<Panel {...props}>
<Profiler id="Content" onRender={callback}>
<Content {...props} />
</Profiler>
<Profiler id="PreviewPane" onRender={callback}>
<PreviewPane {...props} />
</Profiler>
</Panel>
</Profiler>
</App>
);
```

> Note
>
> Although `Profiler` is a light-weight component, it should be used only when necessary; each use adds some CPU and memory overhead to an application.
## `onRender` Callback

The `Profiler` requires an `onRender` function as a prop.
React calls this function any time a component within the profiled tree "commits" an update.
It receives parameters describing what was rendered and how long it took.

```js
function onRenderCallback(
id, // the "id" prop of the Profiler tree that has just committed
phase, // either "mount" (if the tree just mounted) or "update" (if it re-rendered)
actualDuration, // time spent rendering the committed update
baseDuration, // estimated time to render the entire subtree without memoization
startTime, // when React began rendering this update
commitTime, // when React committed this update
interactions // the Set of interactions belonging to this update
) {
// Aggregate or log render timings...
}
```

Let's take a closer look at each of the props:

* **`id: string`** -
The `id` prop of the `Profiler` tree that has just committed.
This can be used to identify which part of the tree was committed if you are using multiple profilers.
* **`phase: "mount" | "update"`** -
Identifies whether the tree has just been mounted for the first time or re-rendered due to a change in props, state, or hooks.
* **`actualDuration: number`** -
Time spent rendering the `Profiler` and its descendants for the current update.
This indicates how well the subtree makes use of memoization (e.g. [`React.memo`](/docs/react-api.html#reactmemo), [`useMemo`](/docs/hooks-reference.html#usememo), [`shouldComponentUpdate`](/docs/hooks-faq.html#how-do-i-implement-shouldcomponentupdate)).
Ideally this value should decrease significantly after the initial mount as many of the descendants will only need to re-render if their specific props change.
* **`baseDuration: number`** -
Duration of the most recent `render` time for each individual component within the `Profiler` tree.
This value estimates a worst-case cost of rendering (e.g. the initial mount or a tree with no memoization).
* **`startTime: number`** -
Timestamp when React began rendering the current update.
* **`commitTime: number`** -
Timestamp when React committed the current update.
This value is shared between all profilers in a commit, enabling them to be grouped if desirable.
* **`interactions: Set`** -
Set of ["interactions"](http://fb.me/react-interaction-tracing) that were being traced the update was scheduled (e.g. when `render` or `setState` were called).

> Note
>
> Interactions can be used to identify the cause of an update, although the API for tracing them is still experimental.
>
> Learn more about it at [fb.me/react-interaction-tracing](http://fb.me/react-interaction-tracing)
Loading

0 comments on commit 6633c1e

Please sign in to comment.