1👍
The general idea of memoizing the class names is fine, but there’s a big problem – you’re re-computing the value every time the component renders because there’s no dependency array. Given your current code, you may as well not be using useMemo
at all.
This is a very inexpensive process, so:
- Either don’t worry about memoization, and just construct the class name string each render (which would be perfectly reasonable)
- Or properly memoize the class name by putting all dependent values into the dependency array
useMemo(() => { }, [this.top, this.middle, this.bottom]); // etc
(do the values you want to check really exist on
this
? That sounds extraordinarily strange in a functional component where you’re using hooks likeuseMemo
. You probably want to reference props or a state value instead.)
Source:stackexchange.com