Christopher Okafor’s Post

𝗦𝘁𝗼𝗽 𝗺𝗮𝗸𝗶𝗻𝗴 𝗰𝘂𝘀𝘁𝗼𝗺 𝗵𝗼𝗼𝗸𝘀 𝗳𝗼𝗿 𝗲𝘃𝗲𝗿𝘆𝘁𝗵𝗶𝗻𝗴 Early in my React journey, I treated custom hooks like a badge of honor. If there was state, I extracted it. If there was logic, I wrapped it. useToggle for a boolean. useCounter for a number. useForm for every input. useFetch for every API call. useLocalStorage for a single value. I was abstracting anything that moved. My /hooks folder was massive. And I thought that meant my code was clean. It wasn't. It was overengineered. Premature abstraction is just as dangerous as premature optimization. A custom hook that wraps a single useState adds zero value. It adds a layer of indirection. It adds a file to navigate. It adds cognitive load for anyone reading your code, including future you. `const [isOpen, setIsOpen] = useState(false)` doesn't need a useToggle hook. It's already clear. It's already simple. Leave it alone. The principle that changed how I think about abstractions: don't abstract until you have to. Duplication is far cheaper than the wrong abstraction. I now have three clear criteria for when a custom hook earns its place: The logic is shared across 3+ components. Not 2, not "maybe later." Three or more, with actual duplication happening right now. It encapsulates a third-party library, so the rest of the application doesn't couple directly to that dependency. It contains business logic that needs to be tested in isolation from the component layer. If it doesn't meet at least one of these criteria, it stays inline. The result? Fewer files. Fewer abstractions. Code that's easier to read, easier to debug, and easier to hand off to another developer. Junior developers abstract to look smart. Senior developers keep things simple because they've seen the cost of not doing so. Write the useState. Inline the logic. Extract only when the codebase demands it, not when your instincts suggest it. Simplicity isn't lazy. It's a skill. #React #JavaScript #TypeScript #WebDevelopment #Frontend #SoftwareEngineering

To view or add a comment, sign in

Explore content categories