28.7 Typing Third-Party Hooks

Alright, let’s talk about the real world. You’re not just writing your own beautiful, perfectly typed hooks; you’re also going to be using other people’s. And let’s be honest, some of those people might have been having a very long day when they wrote the type definitions. Our job is to build a sturdy, type-safe scaffold around their code, even when it feels like they’re actively working against us. The golden rule here is simple: Never use any to shut the compiler up. That’s like fixing a gas leak by removing the smoke alarm. We’re better than that. We’re going to understand what we’re dealing with and apply the correct level of type safety.

28.6 Custom Hook Return Types: Tuple vs Object

Alright, let’s settle a debate that’s less about technical superiority and more about developer ergonomics and how you like your data served: in a neatly wrapped object package or a structured tuple takeout box. When you craft a custom hook, the most important contract you design is its return value. It’s the entire API for anyone using your hook. Get it wrong, and you’ll have a chorus of frustrated developers (or future-you) complaining. TypeScript gives you two primary ways to define this return type: an array/tuple or a plain object.

28.5 useCallback and useMemo: Generic Memoization

Right, let’s talk about useCallback and useMemo. You’ve probably heard they’re for “performance,” and then you saw a dozen articles telling you to wrap everything in them. Please, for the love of all that is holy, don’t do that. They’re not magic performance dust; they’re precision tools with very specific—and honestly, somewhat niche—use cases. Misusing them can actually make your app slower and definitely more complicated. I’m here to give you the straight talk on when and why you’d actually need them.

28.4 useContext: Avoiding undefined with Non-Nullable Context

Right, so you’ve decided to use Context. Good for you. It’s a fantastic tool for passing data around without resorting to what I call “prop-drilling” – the tedious and error-prone practice of threading data through a dozen components that don’t care about it just to get it to the one that does. But here’s the first tripwire you’re going to hit, the one that makes everyone’s TypeScript compiler scream in unison: undefined.

28.3 useRef: Mutable Refs vs DOM Refs

Alright, let’s talk about useRef. This is the hook you reach for when you need to break the rules. React is all about the purity of components, the sanctity of props and state leading to predictable renders. useRef is your backdoor, your escape hatch. It says, “I need to remember something, but I swear, pinky promise, its changing has nothing to do with rendering.” It’s the hook of last resort, and when you need it, you really need it.

28.2 useReducer: Typing Actions and State

Right, so you’ve graduated from the tyranny of ten useState calls for a single component and you’re ready to embrace useReducer. It feels more grown-up, doesn’t it? Like switching from a scooter to a manual transmission. But with TypeScript, you’re not just getting the keys—you’re also getting the full, annotated repair manual. Our job is to make sure that manual doesn’t read like it was translated through three languages. The core idea of useReducer is simple: you give it a reducer function (state, action) => newState and an initial state, and it gives you back the current state and a dispatch function to send actions. TypeScript’s job is to lock this whole system down, making it impossible to dispatch an action you didn’t account for or to access a state property that doesn’t exist. It’s your personal bouncer for state management.

28.1 useState<T>: When TypeScript Can and Cannot Infer the Type

Right, let’s talk about useState and TypeScript. This is where you and the compiler start a beautiful, occasionally pedantic, friendship. The good news is that TypeScript is shockingly good at figuring out what type of state you’re trying to manage. The bad news is that when it can’t, it fails in the most spectacularly unhelpful ways. We’re going to learn how to make it work for us, not against us.

— joke —

...