Skip to content

Commit 39b1eec

Browse files
committed
Add prerequisite background from purity page
1 parent 3b6eb80 commit 39b1eec

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/content/reference/rules/components-and-hooks-must-be-pure.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ This reference page covers advanced topics and requires familiarity with the con
1212

1313
<InlineToc />
1414

15-
---
16-
1715
### Why does purity matter? {/*why-does-purity-matter*/}
1816

1917
React is declarative: you tell React _what_ to render, and React will figure out _how_ best to display it to your user. To do this, React has a few phases where it runs your code. You don't need to know about all of these phases to use React well. But at a high level, you should know about what code runs in _render_, and what runs outside of it.
2018

2119
#### How does React run your code? {/*how-does-react-run-your-code*/}
22-
_Rendering_ refers to calculating what the next version of your UI should look like. After rendering, [Effects](/reference/react/useEffect) are _flushed_ (meaning they are run until there are no more left) and may update the calculation if the Effects have impacts on layout. React takes this calculation and compares it to the previously calculated one, then _commits_ the minimum changes to the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) (what your user actually sees) to catch it up to the latest version.
20+
_Rendering_ refers to calculating what the next version of your UI should look like. After rendering, [Effects](/reference/react/useEffect) are _flushed_ (meaning they are run until there are no more left) and may update the calculation if the Effects have impacts on layout. React takes this new calculation and compares it to the calulation used to create the previous version of your UI, then _commits_ just the minimum changes needed to the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) (what your user actually sees) to catch it up to the latest version.
2321

2422
#### What is purity? {/*what-is-purity*/}
2523
One of the key concepts that makes React, _React_ is _purity_. A pure component or hook is one that is:
2624

2725
* **Idempotent** – You [always get the same result everytime](/learn/keeping-components-pure#purity-components-as-formulas) you run it with the same inputs – props, state, context for component inputs; and arguments for hook inputs.
28-
* **Has no side effects in render** – Code with side effects should run **separately from rendering**: for example as an [event handler](/learn/responding-to-events) – where the user interacts with the UI and causes it to update – or as an [Effect](/reference/react/useEffect) – which runs after render.
29-
* **Does not mutate non-local values**: Components and hooks should never modify values that aren't created locally
26+
* **Has no side effects in render** – Code with side effects should run [**separately from rendering**](#how-does-react-run-your-code): for example as an [event handler](/learn/responding-to-events) – where the user interacts with the UI and causes it to update – or as an [Effect](/reference/react/useEffect) – which runs after render.
27+
* **Does not mutate non-local values**: Components and hooks should never modify values that aren't created locally. Local mutations are covered [below](#mutation).
3028

3129
When render is kept pure, React can understand how to prioritize which updates are most important for the user to see first. This is made possible because of render purity: since components don't have side effects in render, React can pause rendering components that aren't as important to update, and only come back to them later when it's needed.
3230

0 commit comments

Comments
 (0)