Skip to content

Commit 6bb9e96

Browse files
committed
Document React.pure
1 parent 2ff1f9f commit 6bb9e96

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

content/docs/reference-react.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ React components let you split the UI into independent, reusable pieces, and thi
2626

2727
If you don't use ES6 classes, you may use the `create-react-class` module instead. See [Using React without ES6](/docs/react-without-es6.html) for more information.
2828

29+
React components can also be defined as functions which can be wrapped by further functionality.
30+
31+
- [`React.pure`](#reactpure)
32+
2933
### Creating React Elements
3034

3135
We recommend [using JSX](/docs/introducing-jsx.html) to describe what your UI should look like. Each JSX element is just syntactic sugar for calling [`React.createElement()`](#createelement). You will not typically invoke the following methods directly if you are using JSX.
@@ -88,6 +92,42 @@ If your React component's `render()` function renders the same result given the
8892
8993
* * *
9094

95+
### `React.pure`
96+
97+
```javascript
98+
const MyComponent = React.pure(function(props) {
99+
/* render using props */
100+
});
101+
```
102+
103+
`React.pure` is a [higher order component](/docs/higher-order-components.html). It's similar to [`React.PureComponent`](#reactpurecomponent) but for function components instead of classes.
104+
105+
If your function component renders the same result given the same props, you can wrap it in a call to `React.pure` for a performance boost in some cases by memoizing the result.
106+
107+
By default it will only shallowly compare complex objects in the props object. If you want control over the comparison, you can also provide a custom comparison function as the second argument.
108+
109+
```javascript
110+
function render(props) {
111+
/* render using props */
112+
}
113+
function equals(prevProps, nextProps) {
114+
/*
115+
return true if passing nextProps to render would return
116+
the same result as passing prevProps to render,
117+
otherwise return false
118+
*/
119+
}
120+
const MyComponent = React.pure(render, equals);
121+
```
122+
123+
This method only exists as a **[performance optimization](/docs/optimizing-performance.html).** Do not rely on it to "prevent" a rendering, as this can lead to bugs.
124+
125+
> Note
126+
>
127+
> Unlike the [`shouldComponentUpdate()`](/docs/react-component.html#shouldcomponentupdate) method on class components, the `areEqual` function returns `true` if the props are equal and `false` if the props are not equal. This is the inverse from `shouldComponentUpdate`.
128+
129+
* * *
130+
91131
### `createElement()`
92132

93133
```javascript

0 commit comments

Comments
 (0)