useParentElement
Returns the parentElement of a target DOM node as a reactive Observable.
Re-evaluates whenever the target Ref$ or Observable changes.
Targets can be Ref$, MaybeElement, or a plain Element.
Move your mouse over the page to highlight elements.
import { function useRef$<T extends Element = Element>(externalRef?: React.Ref<T> | null): Ref$<T>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$, import useParentElement
useParentElement } from "@usels/core";
function function Component(): React.JSX.Element
Component() { const const el$: Ref$<HTMLDivElement>
el$ = useRef$<HTMLDivElement>(externalRef?: React.Ref<HTMLDivElement> | undefined): Ref$<HTMLDivElement>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$<interface HTMLDivElement
HTMLDivElement>(); const const parent$: any
parent$ = import useParentElement
useParentElement(const el$: Ref$<HTMLDivElement>
el$);
return <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.RefAttributes<HTMLDivElement>.ref?: React.Ref<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={const el$: Ref$<HTMLDivElement>
el$} />;}Reading the parent element
Section titled “Reading the parent element”Use inside an observer component to reactively render based on the parent:
import { function useRef$<T extends Element = Element>(externalRef?: React.Ref<T> | null): Ref$<T>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$, import useParentElement
useParentElement } from "@usels/core";import { function observer<P extends React.FC<any>>(component: P): P
observer } from "@legendapp/state/react";
const const Component: () => React.JSX.Element
Component = observer<() => React.JSX.Element>(component: () => React.JSX.Element): () => React.JSX.Element
observer(() => { const const el$: Ref$<HTMLDivElement>
el$ = useRef$<HTMLDivElement>(externalRef?: React.Ref<HTMLDivElement> | undefined): Ref$<HTMLDivElement>
Creates an observable element ref. Can be used as a drop-in replacement for
useRef, composed with callback refs, or used with forwardRef.
The element is wrapped with opaqueObject to prevent legendapp/state
from making DOM properties reactive (deep observation).
useRef$<interface HTMLDivElement
HTMLDivElement>(); const const parent$: any
parent$ = import useParentElement
useParentElement(const el$: Ref$<HTMLDivElement>
el$);
return <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.RefAttributes<HTMLDivElement>.ref?: React.Ref<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={const el$: Ref$<HTMLDivElement>
el$}>{const parent$: any
parent$.any
get()?.any
tagName}</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>;});With an Observable target
Section titled “With an Observable target”import { function observable<T>(): Observable<T | undefined> (+2 overloads)
observable } from "@legendapp/state";import { import useParentElement
useParentElement } from "@usels/core";
function function Component(): void
Component() { const const target$: any
target$ = observable<HTMLElement | null>(value: Promise<HTMLElement | null> | (() => HTMLElement | null) | (HTMLElement | null)): any (+2 overloads)
observable<interface HTMLElement
HTMLElement | null>(null); const const parent$: any
parent$ = import useParentElement
useParentElement(const target$: any
target$); // ...}With a plain element
Section titled “With a plain element”import { import useParentElement
useParentElement } from "@usels/core";
function function Component({ el }: { el: HTMLElement;}): void
Component({ el: HTMLElement
el }: { el: HTMLElement
el: interface HTMLElement
HTMLElement }) { const const parent$: any
parent$ = import useParentElement
useParentElement(el: HTMLElement
el); // ...}Type Declarations
Section titled “Type Declarations”export declare function useParentElement(element?: MaybeElement): Observable<OpaqueObject<HTMLElement | SVGElement> | null>;Source
Section titled “Source”Contributors
Section titled “Contributors”- tigerwest
Changelog
Section titled “Changelog”a7392ab2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)