Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/metal-times-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/react-form': patch
'@tanstack/react-form-nextjs': patch
---

use React 18's useId hook by default for formId generation, only calling Math.random() as a fallback if no formId is provided.
5 changes: 3 additions & 2 deletions packages/react-form/src/useForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use client'

import { FormApi, functionalUpdate, uuid } from '@tanstack/form-core'
import { FormApi, functionalUpdate } from '@tanstack/form-core'
import { useStore } from '@tanstack/react-store'
import { useMemo, useState } from 'react'
import { Field } from './useField'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
import { useFormId } from './useFormId'
import type {
AnyFormApi,
AnyFormState,
Expand Down Expand Up @@ -184,7 +185,7 @@ export function useForm<
TSubmitMeta
>,
) {
const fallbackFormId = useState(() => uuid())[0]
const fallbackFormId = useFormId()
const [prevFormId, setPrevFormId] = useState<string>(opts?.formId as never)

const [formApi, setFormApi] = useState(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-form/src/useFormId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react'
import { uuid } from '@tanstack/form-core'

/** React 17 does not have the useId hook, so we use a random uuid as a fallback. */
export const useFormId = React.version.split('.')[0] === '17' ? uuid : React.useId