Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Memoize Context provider values
Stable provider values prevent unnecessary re-renders across all Context consumers.
Bad example
| 1 | import React, { createContext, useState } from "react"; |
| 2 |
|
| 3 | type ModalCtx = { |
| 4 | isOpen: boolean; |
| 5 | open: () => void; |
| 6 | close: () => void; |
| 7 | }; |
| 8 |
|
| 9 | export const ModalContext = createContext<ModalCtx | null>(null); |
| 10 |
|
| 11 | export function ModalProvider({ children }: { children: React.ReactNode }) { |
| 12 | const [isOpen, setIsOpen] = useState(false); |
| 13 |
|
| 14 | // New functions + new object every render => all consumers re-render. |
| 15 | const value: ModalCtx = { |
| 16 | isOpen, |
| 17 | open: () => setIsOpen(true), |
| 18 | close: () => setIsOpen(false) |
| 19 | }; |
| 20 |
|
| 21 | return <ModalContext.Provider value={value}>{children}</ModalContext.Provider>; |
| 22 | } |
Explanation (EN)
The provider recreates its value object and functions on every render. That changes the Context value reference, forcing all consumers to re-render even if they don't depend on the changed parts.
Objašnjenje (HR)
Provider stvara novi value objekt i nove funkcije na svakom renderu. Time se mijenja referenca Context vrijednosti i svi consumeri se ponovno renderiraju, cak i ako ne ovise o promjeni.
Good example
| 1 | import React, { createContext, useCallback, useMemo, useState } from "react"; |
| 2 |
|
| 3 | type ModalCtx = { |
| 4 | isOpen: boolean; |
| 5 | open: () => void; |
| 6 | close: () => void; |
| 7 | }; |
| 8 |
|
| 9 | export const ModalContext = createContext<ModalCtx | null>(null); |
| 10 |
|
| 11 | export function ModalProvider({ children }: { children: React.ReactNode }) { |
| 12 | const [isOpen, setIsOpen] = useState(false); |
| 13 |
|
| 14 | const open = useCallback(function openModal() { |
| 15 | setIsOpen(true); |
| 16 | }, []); |
| 17 |
|
| 18 | const close = useCallback(function closeModal() { |
| 19 | setIsOpen(false); |
| 20 | }, []); |
| 21 |
|
| 22 | const value = useMemo<ModalCtx>( |
| 23 | () => ({ isOpen, open, close }), |
| 24 | [isOpen, open, close] |
| 25 | ); |
| 26 |
|
| 27 | return <ModalContext.Provider value={value}>{children}</ModalContext.Provider>; |
| 28 | } |
Explanation (EN)
Callbacks are stable and the provider value is memoized. Consumers only re-render when the actual state they depend on changes.
Objašnjenje (HR)
Callbackovi su stabilni, a provider value je memoiziran. Consumeri se ponovno renderiraju samo kada se stvarno promijeni stanje o kojem ovise.
Exceptions / Tradeoffs (EN)
If the provider has very few consumers and re-renders are rare, memoization may be unnecessary. Confirm with profiling before adding complexity.
Iznimke / Tradeoffi (HR)
Ako provider ima malo consumera i rijetko se rerenderira, memoizacija mozda nije potrebna. Potvrdi profilingom prije dodavanja kompleksnosti.