useId behavior in asynchronous environments #11817
Replies: 3 comments 1 reply
-
I'm not sure, but aren't we supposed to only call use...() functions at the top-level of the setup function? |
Beta Was this translation helpful? Give feedback.
-
I find the implementation and relying on getCurrentInstance anyway slightly weird. https://github.com/vuejs/core/blob/main/packages/runtime-core/src/helpers/useId.ts Why wasn't it written like e.g. with a generator function or a global tracked state. We just want to have general unique (but simple to read) Vue ids that are targeted to be used for the html element id attribute. I'm not sure if it needs to be a tracked reactive, this might be a bit to overkill and to much performance loss. But to extend existing behavior with workaround, we could make it possible to pass a Vue instance and fallback to getCurrentInstance. |
Beta Was this translation helpful? Give feedback.
-
Yes, this is deliberate. one of the expressed purposes of For that to work, ids can only be generated at certain points in the render cycle, specifically during component setup. Otherwise, the could not be stable between server and client and would cause hydration errors. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to start a discussion about the useId composable in Vue 3, specifically its behavior in asynchronous environments. As many of us have experienced, useId doesn't work as expected when called within asynchronous contexts such as setTimeout, setInterval, or Promise callbacks. This is due to its reliance on getCurrentInstance(), which returns null in these scenarios.
Current Behavior
Currently, when useId is called in an async context:
Questions for Discussion
Was the current behavior a deliberate design decision? If so, what were the key considerations?
How are others in the community handling ID generation in async contexts while maintaining the benefits of useId (like SSR compatibility and component scope)?
Would it be beneficial to extend useId to work in async environments? What would be the pros and cons?
If extending useId isn't feasible, what alternatives could we consider? Perhaps a new composable like useAsyncId?
Code Example
Here's a simple example to illustrate the issue:
Beta Was this translation helpful? Give feedback.
All reactions