Design patterns: which ones should you learn, and when should you use them?
Choose a design pattern from a concrete problem: React examples, business logic and a reading path through Patterns.dev.
When knowing names is no longer enough
When I mentored interns and junior developers, one question kept returning: I know MVC, I have heard of Singleton, but where do I actually use them? Recognising a name left the difficulty of choosing a structure in the project's code unresolved.
We would turn to a whiteboard. We drew the components and the data flowing between them. Arrows exposed the problem: a prop passed through several layers without being used, or a business rule was copied across several screens. We could finally discuss a solution based on what was actually getting in the way.
That is the starting point I recommend for learning design patterns: describe the difficulty, then compare possible responses. Here are a few pointers for a JavaScript or React frontend project.
Data passes through too many components: consider Provider
Imagine a display preference used by several components far apart in the React tree. Intermediate components receive this value solely to pass it along. Passing props through multiple levels is often called prop drilling.
The Provider pattern offers another route: put the value in a context, then let the relevant descendants read it with useContext. The Provider defines the part of the tree receiving that value. The cover diagram shows the role of this boundary.
A few props remain easy to follow and make dependencies visible. Before adding context, I also check whether composing the components differently would shorten their path. A Provider becomes useful when intermediate passing really complicates changes. You still need to decide where state lives and who can change it: context carries a value but does not, by itself, define its update rules.
Components repeat a behaviour: extract a focused hook
Two screens need to respond to the same browser event and clean up their subscription when they disappear. If this logic is duplicated, a fix may reach only one screen. A custom hook groups that behaviour behind a name tied to the need, such as useOnlineStatus to track the network connection.
Each call still retains its own local React state. Extracting usePanier and calling it from two components does not automatically give them a shared cart. Sharing the same data requires choosing a state owner and a way to pass it along, or relying on a shared external source.
A function that computes a result without calling a React hook can remain an ordinary function. I reserve the use prefix for abstractions that need it. This avoids unnecessarily coupling a business rule to React.
A business rule is scattered: start with a module
Take a fictional rule: an order can ship when its payment is confirmed and its stock reserved. If every screen copies these conditions, changing them requires finding every variant. I would start by extracting a function into a dedicated module.
type Commande = {
paiement: "en-attente" | "confirme";
stockReserve: boolean;
};
export function peutExpedier(commande: Commande): boolean {
return commande.paiement === "confirme"
&& commande.stockReserve;
}Screens can use this function to display their decision. The rule can be reviewed without walking through JSX and checked with precise inputs. This excerpt illustrates eligibility calculation only; it does not ship anything or represent the full rules of a real business.
Splitting code into modules is sometimes enough to clarify it. If two similar rules serve different business needs, combining them too early may instead make them hard to evolve separately. I first check that they should change together.
Global state becomes confusing: check its scope before Singleton
Singleton aims to provide a single instance within a given scope. It may suit a resource for which multiple instances cause an identified problem. The pattern's name alone therefore does not justify sharing an object across the entire application.
A mutable object accessible everywhere can hide the places that change it and complicate test isolation. On the server, state kept in a module may also survive between requests: storing one user's cart there risks sharing it with other users. Before choosing this, I define the expected lifetime and consumers. A dependency passed explicitly as a parameter may be easier to replace in a test.
Use Patterns.dev starting from a project problem
Patterns.dev is a free resource collecting design, rendering and performance patterns for web applications, with JavaScript and React examples. The site presents its catalogue as a way to recognise possible solutions rather than a checklist to apply everywhere. It is a resource I would have liked to have on hand for those whiteboard discussions.
To begin, I suggest the Provider Pattern and Hooks Pattern pages if you are working on data flow or reusing React logic. The Singleton Pattern page then helps examine the constraints of a shared instance. Links are in the sources. For React APIs, I cross-check examples against the official documentation, especially its explanations of context and custom hooks.
An exercise for your next change
Choose a place in the project that slows down a real change. Before refactoring, write down the problem and draw the data's path. Then compare the current structure with an alternative while preserving the same expected behaviour.
When pairing or mentoring, I would ask the developer to explain where the next rule would be added and what would need checking. If the answer still requires traversing files with no obvious connection, the structure deserves another look. The pattern's name becomes useful when it lets the team discuss that choice and its limitations precisely.