Skip to content

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

Merged
piecyk merged 1 commit intoTanStack:mainfrom
isaackaara:fix/window-example-scroll-margin
Mar 12, 2026
Merged

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

Conversation

@isaackaara
Copy link
Contributor

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

@nx-cloud
Copy link

nx-cloud bot commented Mar 12, 2026

View your CI Pipeline Execution ↗ for commit 754240a

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 8s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 16s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-12 18:54:24 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 12, 2026

More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1142

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1142

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1142

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1142

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1142

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1142

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1142

commit: 4d76010

@piecyk piecyk merged commit e049ad9 into TanStack:main Mar 12, 2026
4 checks passed
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

2 participants