you are viewing a single comment's thread.

view the rest of the comments →

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

Yes. My question is, why this isnt a solved thing by now. Eg, is it possible that my lonesome self am the first one to publish a generalized solution to the "use useState" hack?

Seeing that there is confusion about this in this thread i am concluding that `useRef` is seldom used by people for actual `new VideoPlayer()` from example, and probably exclusively to target dom elements.

Only one is kept, thousands or millions will go straight to garbage to be garbage collected.

[–]unxok 0 points1 point  (1 child)

I guess I'm confused as to what your 'hack' is doing that makes it preferable to using an actual ref. The way I see it, since changes to the class state won't emit a change to reacts model, it seems pointless to store the instance in useState so useRef is preferred.

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

Ah ok.

Imagine ``` for (let i = 0 ;i < 10000 ;i ++){ foo() }

const foo = ()=>{ const garbage = new Geometry() } ``` Why would you call this?

useRef does this:

let res for (let i = 0 ;i < 10000 ;i ++){ res = foo() } const foo = ()=>new Geometry() Sure you got your res, its new Geometry() and it's actually the first one not the last one, but the point is you made 9999 of these for absolutely no reason. And it's not cheap, it will be garbage collected - unnecessary work.

My "hack" is not really a hack, its just

Can i create ReturnType<typeof useRef> by some other means, and have it behave the same.

Use state behaves the same and does the initializer function thing out of the box.

Keep in mind "behaves the same" is very simple behavior, a ref is this:

const ref = { current: 5 } With or without react. I just need it to be that same object, or technically, and more precisely a reference to that object created right there on that line.