Small React Patterns That Save Hours
Shipping small, reliable improvements beats one big rewrite. Here are three tiny patterns I use every week to avoid bugs and refactors.
- Component-level focus styles
Keep focus styles local and consistent by using a small helper class or CSS module. Example (CSS module):
.focus-ring:focus {
outline: 3px solid rgba(99,102,241,0.35);
outline-offset: 2px;
}Then apply where it matters — forms, custom buttons — and you avoid visual regressions while improving accessibility.
- Defensive effect cleanup
When using useEffect with async work, always cancel/ignore stale results so race conditions don't leak state:
useEffect(() => {
let mounted = true;
fetchData().then(result => {
if (!mounted) return;
setData(result);
});
return () => { mounted = false; };
}, [fetchDeps]);This tiny pattern prevents updates after unmount or when parameters change quickly.
- Tiny adapter components for third-party libs
Wrap third-party components in a thin adapter that maps props and isolates version-specific quirks. When the lib changes, only the adapter needs an update — not your entire app.
Closing thought: small, opinionated patterns reduce cognitive load. Ship them as tiny utilities and docs in your repo so the next you (or your teammates) can move faster.
— Mustaque