Home / Function/ IntersectionObserverCase() — react Function Reference

IntersectionObserverCase() — react Function Reference

Architecture documentation for the IntersectionObserverCase() function in IntersectionObserverCase.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  65a8846b_31a5_720d_37ad_044309d2efa4["IntersectionObserverCase()"]
  e6ade876_ee26_9239_190e_a16f84c0b9f9["IntersectionObserverCase.js"]
  65a8846b_31a5_720d_37ad_044309d2efa4 -->|defined in| e6ade876_ee26_9239_190e_a16f84c0b9f9
  style 65a8846b_31a5_720d_37ad_044309d2efa4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fixtures/dom/src/components/fixtures/fragment-refs/IntersectionObserverCase.js lines 25–155

export default function IntersectionObserverCase() {
  const fragmentRef = useRef(null);
  const [items, setItems] = useState(initialItems);
  const addedItems = items.slice(3);
  const anyOnScreen = items.some(([, onScreen]) => onScreen);
  const observerRef = useRef(null);

  useEffect(() => {
    if (observerRef.current === null) {
      observerRef.current = new IntersectionObserver(
        entries => {
          setItems(prev => {
            const newItems = [...prev];
            entries.forEach(entry => {
              const index = newItems.findIndex(
                ([id]) => id === entry.target.id
              );
              newItems[index] = [entry.target.id, entry.isIntersecting];
            });
            return newItems;
          });
        },
        {
          threshold: [0.5],
        }
      );
    }
    fragmentRef.current.observeUsing(observerRef.current);

    const lastFragmentRefValue = fragmentRef.current;
    return () => {
      lastFragmentRefValue.unobserveUsing(observerRef.current);
      observerRef.current = null;
    };
  }, []);

  return (
    <TestCase title="Intersection Observer">
      <TestCase.Steps>
        <li>
          Scroll the children into view, observe the sidebar appears and shows
          which children are in the viewport
        </li>
        <li>
          Add a new child and observe that the Intersection Observer is applied
        </li>
        <li>
          Click Unobserve and observe that the state of children in the viewport
          is no longer updated
        </li>
        <li>
          Click Observe and observe that the state of children in the viewport
          is updated again
        </li>
      </TestCase.Steps>

      <TestCase.ExpectedResult>
        <p>
          Fragment refs manage Intersection Observers on the first level of host
          children. This page loads with an effect that sets up an Inersection
          Observer applied to each child card.
        </p>
        <p>
          New child nodes will also have the observer applied. Removed nodes
          will be unobserved.
        </p>
      </TestCase.ExpectedResult>
      <Fixture>
        <Fixture.Controls>
          <button
            onClick={() => {
              setItems(prev => [
                ...prev,
                [`Extra child: ${prev.length + 1}`, false],
              ]);
            }}>
            Add Child
          </button>
          <button
            onClick={() => {
              setItems(prev => {

Domain

Subdomains

Frequently Asked Questions

What does IntersectionObserverCase() do?
IntersectionObserverCase() is a function in the react codebase, defined in fixtures/dom/src/components/fixtures/fragment-refs/IntersectionObserverCase.js.
Where is IntersectionObserverCase() defined?
IntersectionObserverCase() is defined in fixtures/dom/src/components/fixtures/fragment-refs/IntersectionObserverCase.js at line 25.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free