Originally published at https://www.developerway.com. The website has more articles like this π
Scary title, isnβt it? The sad part is that i...
For further actions, you may consider blocking this person and/or reporting abuse
Great article and explanation, you make it so clear and concise very well done. Just wanted to make sure I understood that the ref isn't needed in the final code snippet.
You mean in
ModalBaseWithAnalytics
? (this is the last one)Or for one of the
ModalBase
before? On<ModalBase
it's always needed, since it passesref
down to the div. OnModalBaseWithAnalytics
it's not needed, since the ref is created inside of this componentThe article in general is great, covers a lot of mistakes devs can make, just one thought that can easily solve the problems:
Generating componenets dynamically inside components/hooks is bad practice. Should be avoided in almost every case. Instead the Dialog component can be separated and it can expose API through its ref. This is a method a lot of 3rd party libraries are using for performance reasons.
I agree in general, creating components is usually quite bad :) But in the article most of the performance problems come not from it, but using hooks in not the most optimal way
Yes, that's just a sidenote :) However, having state management inside the component and exposing the API through ref resolves the performance concerns.
Nice article. π But I Never thought about putting rendered components inside hooks like your modal. For me they were always a way eiyher to access context or reuse logic.
Good, then you don't have to fix them :D Creating components in render is usually terrible. It's only okay for something like modals, when we drop the DOM after it's closed.
I don't think that its a good idea putting a component inside a custom hook, either with a
useMemo
or without it. You're creating a new component each time it must be reevaluated, I think, instead of just passing new props and rendering just what's necessary.Hooks are meant to decouple the from your components, not for putting them inside hooks. Behavior of hooks inside of hooks is predictable, I think, because they have the same behavior inside the custom hook that at component level
Thank for the greact article. Just a quick question.
What benefit below memoization in useModal hook provides?
return useMemo(
() => ({
isOpen,
Dialog,
open,
close,
}),
[isOpen, Dialog, open, close],
I don't understand how it prevents host component from rerendering.
It doesn't, that is the problem. Regardless of what we do, it will re-render.
got it. thanks
Probably, the only final change you need is to put [] in your useEffect as a second parameter. For me it works fine here codesandbox.io/s/re-renders-bad-wi...
Excellent article π Thanks for sharing π
Author came up with some weird technique of using hooks and proved that it is bad. Article is useless
Why create one when you can get all awesome hooks in a single library?
Try scriptkavi/hooks. Copy paste style and easy to integrate with its own CLI
Interesting article definitely something worth considering when creating custom hooks.