Skip to content

Pre-bound Field Components are not typesafe ? (React) #1240

Answered by crutchcorn
atersolis asked this question in General
Discussion options

You must be logged in to vote

You can do this actually very easily yourself by adding a value field that goes unused:

import { createFormHook, createFormHookContexts } from "@tanstack/react-form";

function NumberField({ label }: { label: string, value: number }) {
  const field = useFieldContext<number>();
  return (
    <label>
      <div>{label}</div>
      <input
        value={field.state.value}
        onChange={(e) => field.handleChange(e.target.valueAsNumber)}
      />
    </label>
  );
}

const { fieldContext, formContext, useFieldContext } = createFormHookContexts();
const { useAppForm } = createFormHook({
  fieldContext,
  formContext,
  fieldComponents: {
    NumberField: NumberField,
  },
  formComponents…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by crutchcorn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants