S
SolidJS6mo ago
xxttt

Cleanup function

https://stackblitz.com/edit/solidjs-templates-unzazc?file=src%2FuseFloating.tsx 1. When you hover over the box, you'll see three console logs with the message "Update." 2. If you scroll while your mouse is over the box, you'll notice that it's reacting and automatically updating. This is because the autoUpdate function with the elementResize option returns a ResizeObserver. 3. When you move the mouse away from the box and try to scroll the page, you'll see that there are no more logs. This indicates that the cleanup has occurred. The question is: Why does the update function get invoked three times? I believe the issue might be in the second createEffect. I tried to implement cleanup at the first level, but it didn't work well. Is there a way to improve this behavior? I treid to make cleanup fn for days, but I just don't understand where is the problem now. Thanks!
16 Replies
Danny
Danny6mo ago
I'm not really familiar with floating-ui but from what I can see, autoUpdate() calls the update funtion 3 times by itself if you do nothing. If you immediatly call the clean up function, it is only called once.
xxttt
xxtttOP6mo ago
Yes, I know, but the problem is that I cannot cleanup fn right away. I tried to copy Vue library, but let variable is not reactive. So I just don't know what to do.
Madaxen86
Madaxen866mo ago
What’s the problem of calling it 3x times?
xxttt
xxtttOP6mo ago
It shouldn't work that way :D. And it is bad for old devices.
bigmistqke
bigmistqke6mo ago
pretty confusing code... what is the goal of @floating-ui's autoUpdate?
lxsmnsyc
lxsmnsyc6mo ago
floating-ui is pretty confusing alright. Popper is a bit more "exactly what I need" package, Floating UI is too bare bones, it's "here, you manage it" package
xxttt
xxtttOP6mo ago
https://floating-ui.com/docs/autoupdate you can read more about it here or hover over its options.
autoUpdate | Floating UI
A JavaScript library to position floating elements and create interactions for them.
xxttt
xxtttOP6mo ago
I don't think it is confusing. React library for floating-ui are 😄
bigmistqke
bigmistqke6mo ago
I think it has something to do with that autoUpdate call, if you simply call update inside whileElementsMounted it is called once. if I do const cl = autoUpdate(refrence, floating, update, { elementResize: false }) it is called once too. so it has something to do with the resizing of refrence/float
xxttt
xxtttOP6mo ago
No way
xxttt
xxtttOP6mo ago
Well, in Vue its called two times ;/
No description
xxttt
xxtttOP6mo ago
and updates almost 2 times more compared to Solid.
No description
No description
xxttt
xxtttOP6mo ago
I think that there's no way to optimize it. Is there's a way to make cleanup() fn reactive at the top level somehow? Just like that. Or it is better to tie it with isOpen() in createEffect? What do you think?
No description
mdynnl
mdynnl6mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
mdynnl
mdynnl6mo ago
though you might wanna look at stores for granularly updating along the data
Madaxen86
Madaxen866mo ago
And I'll just throw this in: There's https://kobalte.dev A great headless ui library providing unstyled components that do exactly what floating-ui does. But everything is done the solid way. If you want more default styling there's https://www.solid-ui.com/docs/components/ which is build on top of kobalte.dev using tailwind (it's like shadcn but for solidjs).
solid-ui
Beautifully designed components built with Kobalte and Tailwind CSS.
Want results from more Discord servers?
Add your server