R
roblox-ts2mo ago
May

Retain instances in Viewport Frame with React

I would like to parent an instance to a viewport frame and ensure that it does not get destroyed when the parent element (a react viewport frame component) repaints. I am not very familiar with how repainting works or if it is even possible for the object to be destroyed unless the parent element is unmounted. What could I do to ensure the object does not get destroyed unless the parent viewport frame unmounts? Just use clones I can not use clones, it's an instance referenced by the server and I would like any attribute changes to be mirrored to the client. Although I know I can just clone the instance anyways and watch for attribute changes on the original instance, the purpose of this post is to ask whether there is any way to achieve the goal and parent the original instance to the viewport frame component. Thank you in advance for the help.
18 Replies
OverHash
OverHash2mo ago
Hmm, spitballing here. Maybe having a memo component below which does the creation would work? That way it never gets re-rendered I don't think that :Destroy() is called on the instances, so you could maybe also have something like a useMemo (with no dependencies) that simply creates the instance, and then you can parent that every time in something like a useLayoutEffect or useEffect?
Tester
Tester2mo ago
Wait, useLayourEffect exists in Roblox react?
OverHash
OverHash2mo ago
pretty sure..
Tester
Tester2mo ago
Could you remind me when it's activated? use effect is only after render completed
OverHash
OverHash2mo ago
I went looking and I genuinely don't know sorry I can trace to around here but not much further https://github.com/jsdotlua/react-lua/blob/df4b4072e3aa78fc64850f8ca8b696084f305d75/modules/react-reconciler/src/ReactFiberHooks.new.luau#L1369 maybe @Brooke knows better I've seen some peoples hooks use it, so it must work somewhere there is https://react.luau.page/api-reference/react/#reactuselayouteffect to read i guess
May
MayOP2mo ago
how would that ensure that the child object does not get destroyed on repaints/rerenders? oh i see. so then react doesn't destroy elements on rerenders and only edits their values? is that correct?
OverHash
OverHash2mo ago
Because it would never rerender Sort of yeah, it's just changing the dom tree
May
MayOP2mo ago
ohh, you mean have the memo return a viewport frame jsx?
OverHash
OverHash2mo ago
pretty much yeah. the parent might re-render but the child shouldn't iirc might be a bit rusty on that stuff though
May
MayOP2mo ago
Hmm… would that work though? Doesn’t JSX only cause react to create an element if it’s rendered? which would create the same exact issue as we had in the beginning no?
OverHash
OverHash2mo ago
might have to play around with your problem when i get some spare time haha
May
MayOP2mo ago
very fair lol! also im sure other people have used viewport frames with react, so i wonder how they go about doing it safely? i guess most people just clone the instances (from what i can tell by looking at other posts here)
OverHash
OverHash2mo ago
yup
Tester
Tester2mo ago
Yeah, I do it sometimes, btw here's the info about useLayourEffect
No description
Tester
Tester2mo ago
So it could be the thing you're looking for
OverHash
OverHash2mo ago
a lot of that LLM stuff is gonna be somewhat useless since this is roblox and not the web dom
Tester
Tester2mo ago
Fair enough, I think if the functionality is the same prob it could work

Did you find this page helpful?