How to not render <Portal> if mount is undefined?
It seems <Portal> always insert to document.body if the mount is null.
But I only want to render the mount has element.
3 Replies
You can wrap the Portal in a Show. Passing the element to the when attribute to ensure that it is not null.
Yes, but Can I make document.getElementById reactive?
document.getElementById
only returns an element at a particular moment in time. You could run getElementById
inside createEffect
and then update a signal with the result:
Its worth noting that if foo gets removed from the DOM for whatever reason, your signal will not be updated of this unless the effect runs again.
Alternatively you could use the ref
inherent attribute, but this only works for elements created with JSX: