Skip to content

Document Query Selector replacement with ref if possible #3

@utkuakyuz

Description

@utkuakyuz

medium

Using document.querySelector to get a reference to the list container can be brittle. A more idiomatic and robust React approach is to use refs. The react-window List component provides an outerRef prop for this purpose.

You can refactor this by:

  1. Creating a ref for the list container:
    const listContainerRef = useRef<HTMLDivElement>(null);
  2. Passing it to the List component:
    <List
      // ... other props
      outerRef={listContainerRef}
    >
      {ViewerRow}
    </List>
  3. Using the ref in your useEffect:
    const listContainer = listContainerRef.current;
    if (listContainer) {
      // ...
    }

This avoids querying the global document and makes the component more self-contained and predictable.

Originally posted by @gemini-code-assist[bot] in #2 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions