Skip to content

[Bug] Text ref churn turns temporary update loops into React error #185 #5264

Closed

Description

@nynexman4464

Problem

Text creates a new merged callback ref every time it renders:

ref={mergeRefs(ref, truncation.ref, textRef)}

When a parent re-renders, React detaches the previous callback ref and attaches the new one. The detach reaches useTruncation with null, and its cleanup branch schedules two state updates even when maxLines is 0.

This can turn a temporary consumer update loop into a fatal React error #185 before asynchronous data has time to stabilize.

Standalone reproduction

The reproduction uses only React 19.2, React DOM 19.2, @astryxdesign/core, and esbuild.

import React, {useEffect, useState} from 'react';
import {createRoot} from 'react-dom/client';
import {Text} from '@astryxdesign/core/Text';

const LOADED_ROWS: string[] = [];

function useDelayedRows() {
  const [data, setData] = useState<string[] | undefined>();

  useEffect(() => {
    const timer = setTimeout(() => setData(LOADED_ROWS), 100);
    return () => clearTimeout(timer);
  }, []);

  return data ?? [];
}

function Repro({astryx}: {astryx: boolean}) {
  const rows = useDelayedRows();
  const [copy, setCopy] = useState<string[]>([]);
  const [renders, setRenders] = useState(0);

  useEffect(() => {
    setCopy(rows);
    setRenders(count => count + 1);
  }, [rows]);

  const content = `rows: ${copy.length}; renders: ${renders}`;
  return astryx ? <Text type="body">{content}</Text> : <span>{content}</span>;
}

createRoot(document.getElementById('root')!).render(<Repro astryx />);

The fresh [] fallback is an intentional consumer bug. It loops only until the 100 ms timer supplies stable data.

Rendered element Result
Plain <span> Timer runs and the page recovers
Astryx <Text> React error #185; root unmounts before the timer runs

This reproduces in both development and production React builds. It reproduces in every stable Astryx release tested: 0.0.15, 0.1.9, 0.2.0, 0.3.0, 0.4.0, 0.4.1, and 0.4.5.

The consumer still has a bug and must fix its unstable fallback. Astryx should not make that temporary loop fail synchronously before the asynchronous operation can complete.

Regression from the predecessor implementation

The predecessor text component kept its merged callback ref stable with useCallback. Astryx changed that to an inline mergeRefs(...) call.

Proposed fix

Memoize the merged ref:

const mergedRef = useMemo(
  () => mergeRefs(ref, truncation.ref, textRef),
  [ref, truncation.ref],
);

Then render ref={mergedRef}.

Verified against the standalone production reproduction:

Build Result
Stock 0.4.5 React #185; root unmounted
Memoized merged ref Timer runs; page survives
Stock restored React #185 again

Heading uses the same inline merged-ref pattern and should be fixed and covered at the same time.

Test gap

Current component tests mount the text but do not rerender it while asserting ref stability. A regression test should rerender Text and Heading with unchanged refs and assert that their ref callbacks are not detached and reattached.

Activity

  1. nynexman4464 commented on Aug 20, 2026

    @nynexman4464
    ContributorAuthor

    Implementation is in draft PR #5266. It now adds a public useMergedRefs hook, moves Text and Heading onto it, and adds a no-inline-merge-refs lint warning. Full lint currently identifies 43 existing inline JSX callsites for follow-up migration; the rule stays non-blocking until those are cleaned up.

  2. nynexman4464 commented on Aug 20, 2026

    @nynexman4464
    ContributorAuthor

    The implementation is now split into three draft PRs: #5266 adds useMergedRefs and fixes Text/Heading; #5267 migrates the remaining core and lab callsites; #5269 enforces the safe pattern with an ESLint error.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions