Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Observe sibling nodes with Fragment refs

Attach one IntersectionObserver to sibling elements with a React Fragment ref and keep both list items direct children of their list.

You want to know which rows are visible in a scrolling list, including a row rendered by another component. Adding a wrapper inside the list would change which elements are direct children of the ul. A Fragment ref lets you observe those children in place.

Observe the rows as a group

Use the explicit <Fragment ref={rowsRef}> form. React puts a FragmentInstance in the ref, so you can call observeUsing once with your observer. Give the observer the ul as its root to measure against that scrolling area.

Use React and React DOM 19.3 or later. Add this to a React DOM entry point with a root element in the HTML. The observer starts reporting a row as visible when at least half of it is inside the list’s scroll area.

import { Fragment, useEffect, useRef, useState } from 'react'
import { createRoot } from 'react-dom/client'
import './methods.css'

function PickupRow() {
  return <li id="pickup"><strong>Pickup</strong></li>
}

function DeliveryOptions() {
  const listRef = useRef(null)
  const rowsRef = useRef(null)
  const [visible, setVisible] = useState('none')

  useEffect(() => {
    const rows = rowsRef.current
    const current = new Set()
    const threshold = 0.5

    const observer = new IntersectionObserver((entries) => {
      for (const { target, intersectionRatio } of entries) {
        if (intersectionRatio >= threshold) current.add(target.id)
        else current.delete(target.id)
      }
      setVisible([...current].sort().join(', ') || 'none')
    }, { root: listRef.current, threshold })

    rows.observeUsing(observer)
    return () => rows.unobserveUsing(observer)
  }, [])

  return (
    <main>
      <p>Visible rows: {visible}</p>
      <ul ref={listRef} className="methods" aria-label="Delivery methods" tabIndex={0}>
        <Fragment ref={rowsRef}>
          <li id="shipping">Shipping</li>
          <PickupRow />
        </Fragment>
      </ul>
    </main>
  )
}

const root = createRoot(document.getElementById('root'))
root.render(<DeliveryOptions />)

The list has two direct li children, including the one returned by PickupRow.

Save these styles as methods.css to make the list one row high.

.methods {
  height: 10rem;
  overflow: auto;
  margin: 0;
  padding: 0;
}

.methods > li {
  height: 10rem;
  display: grid;
  place-items: center;
}

The status starts at shipping once the observer delivers its first entries, then reads pickup when you scroll to the bottom.

Find the first DOM element on each branch

observeUsing registers the Fragment’s first-level DOM children, including those rendered through child components. For PickupRow that is li#pickup. The strong inside it sits one DOM level deeper, so it never appears as an observer target in this example.

diagram

Where the Fragment ref stops

The list is the observer root, the two list items are its targets, and the strong element is inside the pickup target.

The observer callback delivers initial measurements and updates for targets that cross a threshold or change intersection state. A batch may describe a single row, so keep the other rows’ last known state in the set. The cleanup uses the same Fragment instance and observer captured when the effect ran.

If the ref has no observeUsing method, check that it points to the explicit Fragment and that React and React DOM both support Fragment refs.

If you need to measure the strong itself, give that element its own ref and call observer.observe on it. Leave the Fragment ref at the list-item level when the row is the unit you care about.