Skip to content

docs: clarify JSX equality #7789

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions src/content/learn/keeping-components-pure.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ You could think of your components as recipes: if you follow them and don't intr

<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" />

#### JSX Equality:

For two JSX values to be considered equal in React's purity model:

- **Type equality**: They must be the same component or HTML tag (e.g., `<Button />` vs `<div>`).
- **Prop equality**:
- Primitive props (`string`, `number`, `boolean`) must be identical (`===`).
- Object/array props must have *equivalent structure* (same keys/values, even if references differ).
- **Special props**: `key` and `ref` (if present) must be identical.

<Sandpack>

```jsx
// These are considered equal (same structure):
<Child config={{ color: 'red' }} />
<Child config={{ color: 'red' }} />

// These are NOT equal (different structure):
<Child config={{ color: 'red' }} />
<Child config={{ color: 'blue' }} />
```
</Sandpack>

## Side Effects: (un)intended consequences {/*side-effects-unintended-consequences*/}

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!
Expand Down
Loading