Skip to content
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
36 changes: 36 additions & 0 deletions src/content/reference/rules/components-and-hooks-must-be-pure.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,42 @@ function ExpenseForm() {
// Continue rendering...
}
```
This is fine because the mutation is local and doesn't affect other components.

Problematic case:
```js
function SubscriptionExample() {
let subscriptionRef = useRef(null);
// 🔴 Don't do this during render
if(subscriptionRef.current === null) {
subscriptionRef.current = store.subscribe(() => {
console.log("Store Changed")
});
}
useEffect(() => {
return () => {
subscriptionRef.current?.unsubscribe();
}
},[]);
}
```
This is a problematic use of lazy initialization because it perfroms an external side effect during render. If React abandons the render, the subscription may remain active without its cleanup .

Correct approach:
```js
function SubscriptonExample() {
const subscriptionRef = useRef(null);
useEffect(() => {
subscriptionRef.current = store.subscribe(() => {
// ..
})
return () => {
subscriptionRef.current?.unsubscribe();
}
},[]);
}
```
For resources that require setup and cleanup, initialize them inside an Effect.

#### Changing the DOM {/*changing-the-dom*/}

Expand Down
Loading