Skip to content

Commit f86fe8c

Browse files
authored
Update keeping-components-pure.md
1 parent 022e047 commit f86fe8c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/content/learn/keeping-components-pure.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ You could think of your components as recipes: if you follow them and don't intr
8585

8686
<Illustration src="/images/docs/illustrations/i_puritea-recipe.png" alt="A tea recipe for x people: take x cups of water, add x spoons of tea and 0.5x spoons of spices, and 0.5x cups of milk" />
8787

88+
### JSX Equality and Purity
89+
90+
React considers two JSX values to be "equal" for purity purposes if:
91+
1. **Their types are identical** (e.g., both `<Child />`).
92+
2. **Their props are deeply equal**:
93+
- Primitive props (`string`, `number`, `boolean`) must be `===` equal.
94+
- Object/array props must be *structurally equivalent* (same keys/values, regardless of reference).
95+
3. **Their `key` and `ref` props (if any) are equal**.
96+
97+
#### Example
98+
<Sandpack>
99+
100+
```jsx
101+
// These are considered equal (same structure):
102+
<Child config={{ color: 'red' }} />
103+
<Child config={{ color: 'red' }} />
104+
105+
// These are NOT equal (different structure):
106+
<Child config={{ color: 'red' }} />
107+
<Child config={{ color: 'blue' }} />
108+
```
109+
110+
</Sandpack>
111+
88112
## Side Effects: (un)intended consequences {/*side-effects-unintended-consequences*/}
89113

90114
React's rendering process must always be pure. Components should only *return* their JSX, and not *change* any objects or variables that existed before rendering—that would make them impure!

0 commit comments

Comments
 (0)