Skip to content

fix(examples): use layoutEffect ref for scrollMargin in window example#1142

Open
isaackaara wants to merge 1 commit intoTanStack:mainfrom
isaackaara:fix/window-example-scroll-margin
Open

fix(examples): use layoutEffect ref for scrollMargin in window example#1142
isaackaara wants to merge 1 commit intoTanStack:mainfrom
isaackaara:fix/window-example-scroll-margin

Conversation

@isaackaara
Copy link

The window example reads listRef.current?.offsetTop directly during render, which triggers the react-hooks/refs lint rule in eslint-plugin-react-hooks v7 (Compilation Skipped: Cannot access refs during render).

The fix follows the same pattern already used in the dynamic example: capture the offset in a plain ref via useLayoutEffect and use that ref's value as scrollMargin.

const listOffsetRef = React.useRef(0)

React.useLayoutEffect(() => {
  listOffsetRef.current = listRef.current?.offsetTop ?? 0
}, [])

const virtualizer = useWindowVirtualizer({
  // ...
  scrollMargin: listOffsetRef.current,
})

Fixes #1116

Accessing listRef.current during render triggers the react-hooks/refs
ESLint rule in eslint-plugin-react-hooks v7. Use the same pattern as
the dynamic example: store the offset in a separate ref via useLayoutEffect.

Fixes TanStack#1116
@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

⚠️ No Changeset found

Latest commit: 754240a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESLint error in window example

1 participant