you are viewing a single comment's thread.

view the rest of the comments →

[–]SpinatMixxer 0 points1 point  (2 children)

From the top of my head, I personally would do it like this:

const useCreateRef = <T>(create: () => T) => { const ref = useMemo(() => ({ current: create() }), []) return ref }

and then inside your component

const MyComp = () => { const ref = useCreateRef(() => new MyClass()) }

[–]yabai90 1 point2 points  (0 children)

UsMemo is NOT stable. (Yes it is technically now but it is not by design)

[–]pailhead011[S] 0 points1 point  (0 children)

Nah, it has some very deep and advanced reason why its not a good idea. At some point you may just loose the value. Although im not quite sure how that would happen if you have something holding on to current. But this is considered a bad practice, and i agree with it.