Skip to content
Source
Sign inSign up
TanStack/form

Fix react compiler #1893

Mergedcrutchcorn(GitHub profile, opens in a new tab)merged 7 commits intomain fromrefs/pull/1893/headmerged by crutchcorn(GitHub profile, opens in a new tab)

This PR fixes issues with the React compiler where there were edgecases we'd hit previously.

Closes: https://github.com/TanStack/form/issues/1832

    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: a3da6efc69ab53a9145a1cdade88ec77d18fe5a8

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 13 packages
      NameType
      @tanstack/react-formPatch
      @tanstack/form-corePatch
      @tanstack/react-form-nextjsPatch
      @tanstack/react-form-remixPatch
      @tanstack/react-form-startPatch
      @tanstack/angular-formPatch
      @tanstack/form-devtoolsPatch
      @tanstack/lit-formPatch
      @tanstack/solid-formPatch
      @tanstack/svelte-formPatch
      @tanstack/vue-formPatch
      @tanstack/react-form-devtoolsPatch
      @tanstack/solid-form-devtoolsPatch

      Not sure what this means? Click here to learn what changesets are.

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

    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit a3da6efc69ab53a9145a1cdade88ec77d18fe5a8

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded34sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded<1sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-12-01 18:36:40 UTC

    3. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-form

      npm i https://pkg.pr.new/@tanstack/angular-form@1893

      @tanstack/form-core

      npm i https://pkg.pr.new/@tanstack/form-core@1893

      @tanstack/form-devtools

      npm i https://pkg.pr.new/@tanstack/form-devtools@1893

      @tanstack/lit-form

      npm i https://pkg.pr.new/@tanstack/lit-form@1893

      @tanstack/react-form

      npm i https://pkg.pr.new/@tanstack/react-form@1893

      @tanstack/react-form-devtools

      npm i https://pkg.pr.new/@tanstack/react-form-devtools@1893

      @tanstack/react-form-nextjs

      npm i https://pkg.pr.new/@tanstack/react-form-nextjs@1893

      @tanstack/react-form-remix

      npm i https://pkg.pr.new/@tanstack/react-form-remix@1893

      @tanstack/react-form-start

      npm i https://pkg.pr.new/@tanstack/react-form-start@1893

      @tanstack/solid-form

      npm i https://pkg.pr.new/@tanstack/solid-form@1893

      @tanstack/solid-form-devtools

      npm i https://pkg.pr.new/@tanstack/solid-form-devtools@1893

      @tanstack/svelte-form

      npm i https://pkg.pr.new/@tanstack/svelte-form@1893

      @tanstack/vue-form

      npm i https://pkg.pr.new/@tanstack/vue-form@1893

      commit: a3da6ef

    4. codecov[bot](GitHub profile, opens in a new tab)commented

      Codecov Report

      :x: Patch coverage is 88.63636% with 5 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 90.12%. Comparing base (6892ed0) to head (a3da6ef). :warning: Report is 72 commits behind head on main.

      Files with missing linesPatch %Lines
      packages/react-form/src/useForm.tsx70.58%4 Missing and 1 partial :warning:
      Additional details and impacted files
      @@            Coverage Diff             @@
      ##             main    #1893      +/-   ##
      ==========================================
      - Coverage   90.35%   90.12%   -0.23%     
      ==========================================
        Files          38       41       +3     
        Lines        1752     1884     +132     
        Branches      444      473      +29     
      ==========================================
      + Hits         1583     1698     +115     
      - Misses        149      166      +17     
        Partials       20       20              

      :umbrella: View full report in Codecov by Sentry. :loudspeaker: Have feedback on the report? Share it here.

      :rocket: New features to boost your workflow:

      • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
      • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
  • TkDodo reviewed

    TkDodocommented

    note: for the compiler to work, this write to a ref should be in an effect that executes on each render:

    const optsRef = useRef(opts)
    useEffect(() => {
      optsRef.current = opts
    }
    TkDodocommented

    creating an instance in memo is risky because memo is a perf optimization, not a semantic guarantee. The best thing would probably be to write to create fieldApi in react state, then either update it in an effect or re-create it during render. This would also help us get rid of the optsRef altogether I think:

    const [prevOptions, setPrevOptions] = useState(() => ({ form: opts.form, name: opts.name })
    const [fieldApi, setFieldApi] = useState(() => new FieldApi(...))
    
    if (reactiveOptions.form !== opts.form || reactiveOptions.name !== opts.name) {
      setFieldApi(new FieldApi(...))
      setPrevOptions({ form: opts.form, name: opts.name })
    }

    This is the new get-derived-state-from-props pattern described here:

  • Would useLayoutEffect work okay here?

  • TkDodocommented

    shouldn’t matter if we only use the ref in this component, but I think with my below suggestion, we can completely get rid of the ref unless it’s used somewhere else that I am not seeing

  • @TkDodo ready for re-review! Thanks for your help here

  • TkDodo approved these changes

    TkDodocommented

    lovely 🎉

  • Sign in to join this conversation.