Skip to content

Crash in useSelectableCollection when ref not attached to dom node in first render #1622

@stefanprobst

Description

@stefanprobst

🐛 Bug Report

When the ref passed to useSelectableCollection is not yet attached to a DOM node in first render - and if autoFocus is set to truthy - a crash with TypeError: element is undefined happens in this useEffect, which triggers focusSafely => focusWithoutScrolling which then tries to call focus() on null.

🤔 Expected Behavior

No errors.

😯 Current Behavior

see above

💁 Possible Solution

🔦 Context

I ran into this issue when moving useListBox up a level in the useSelect docs example, i.e. when the listbox ref is only attached when state.isOpen becomes true.

💻 Code Sample

CSB was acting up, so i put a repro here: https://github.com/stefanprobst/react-aria-issue/blob/main/components/Select.js

EDIT: here's a codesandbox based on the useSelect docs example: https://codesandbox.io/s/react-aria-listbox-ref-r6elr?file=/src/App.js

🌍 Your Environment

Software Version(s)
react-spectrum latest
Browser Firefox 85
Operating System Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions