🤯 Do you want a pro React tip? I started naming my useEffect functions, and its beautiful 👇 React code is full of hooks noise like useState, useEffect, useRef, and useMemo everywhere. It's hard to quickly scan a file and understand what's actually happening because the lifecycle stuff dominates everything. I started using named functions instead of arrow functions for my effects, and it made a massive difference. Here's why: 1️⃣ Cuts through the noise — When you have multiple useEffects in a component, descriptive names like synchronizeMapZoomLevel or fetchUserData let you scan the file and immediately understand the flow without reading implementation details. 2️⃣ Stack traces — If something breaks, the function name shows up in the error stack. Way easier to debug than an anonymous function at line 47. 3️⃣ Forces single responsibility — When you try to name an effect and struggle? That's usually because it's trying to do too many things. It naturally pushes you to split things up or remove them altogether (You might not need an effect) Some people prefer to extract everything into custom hooks immediately, which is great too. But this works really well for simpler cases where a full hook feels like overkill. Have you tried this? Or do you go straight to custom hooks for everything? #React #JavaScript #WebDev #CleanCode
An alternative approach is to not use `useEffect` or other hooks inside your components anymore, but give the hooks proper names. In your case, creating a hook called e.g. `useSynchronizedMapZoomLevel` would do the trick as well and could potentially make it easier to reuse the hook inside other components. There is a quite interesting blog post by Kyle Shevlin about this idea, which even proposes to enforce this pattern using eslint: https://kyleshevlin.com/use-encapsulation/
You can take this even further: make an "effects" module - exporting effect functions - import the functions - then just do, e.g. useEffect(synchronizeMapZoomLevel, [zoomLevel]). Also, sometimes it's worth creating custom hooks. In my view - it's enough that you use a hook function more than once to justify making it a custom hook. Of-course, it depends if it's something that needs to be run after paint - then it has to be in useEffect. But yeah abstractions makes everything clearer - always think - can I "abstract" it higher?
Great tip! This is better than breaking out every useEffect into a custom hook, and more elegant and declarative than putting a code comment block to explain what the hook does. It also naturally encourages your useEffect hook functions to be focused and not do too much.
Did I just read something who not mention AI ? Is this even possible ?
I'd prefer creating custom hooks. Some interesting "use" cases to consider when defining your Custom Hooks: building portable UI utilities, managing global state, managing server state, encapsulating business logic, etc. Dan Neciu
synchronizeMapZoomLevel is not called after the definition inside useEffect. am i wrong? please check the syntax..
If you really wanna nerd out, you can create a custom ESLint rule for this: https://eslint.org/docs/latest/extend/custom-rule-tutorial
I had a mentor who taught me a lot of these ideas early on in my career before arrow functions were a thing. The same principles apply to anonymous functions. Not only does naming the functions make it significantly easier to read and organize the code, it also makes it easier to debug it because of the stack traces like you mentioned.
https://neciudan.dev/subscribe