Skip to content

Using reference for parentSelector causes "Cannot read property 'ownerDocument' of null" error #983

Closed
@tuckbarber

Description

@tuckbarber

Summary:

I believe I found a bug the happens when the parentSelector prop function is set by reference, in cases where the parentSelector is an element within the page, and a parent is unmounted suddenly.

This is documented and addressed before (here , and here), but I think I am running into a wall caused by a since added update

Since I am passing the parent node via function return

parentSelector={ () => modalParentRef.current} 

the new check

(parentSelector && parentSelector().ownerDocument) || document

throws an error "Cannot read property 'ownerDocument' of null" when the existing node referenced by modalParentRef.current is removed (because of navigation or parent unmount). This is because parentSelector is still a defined function, but returns null.

I have tried a number of workarounds, such as

parentSelector={
    modalParentRef.current
        ? () => modalParentRef.current
        : undefined
}

but I think the prop does not update in time.

My current workaround is

parentSelector={
    (modalParentRef.current || { ownerDocument: undefined, contains: () => false }) as HTMLElement
}

which is unseemly at best, but it works as I would expect.

Steps to reproduce:

  1. Use a Ref in the parentSelector prop e.g. parentSelector={() => someRef.current}

  2. Unmount a parent containing a modal

  3. Get crash and "Cannot read property 'ownerDocument' of null" error

Expected behavior:

I would expect the Modal to realize the return node no longer exists, and handle it that way. My simple and humble suggestion only based on #965 would be to check something like

(parentSelector && parentSelector() && parentSelector().ownerDocument) || document

but there may be a better fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions