CodShot
TSX Public

Accessible Form Fields with useId

Generate stable IDs for reusable form controls and accessible help text.

#react #accessibility #form #typescript
TSX
import { useId } from 'react';

type TextFieldProps = {
  label: string;
  name: string;
  helpText?: string;
};

export function TextField({
  label,
  name,
  helpText,
}: TextFieldProps) {
  const inputId = useId();
  const helpId = `${inputId}-help`;

  return (
    <div>
      <label htmlFor={inputId}>{label}</label>

      <input
        id={inputId}
        name={name}
        aria-describedby={helpText ? helpId : undefined}
      />

      {helpText && (
        <small id={helpId}>
          {helpText}
        </small>
      )}
    </div>
  );
}

Snippet actions

CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support