TanStackT
TanStack7mo ago
3 replies
rubber-blue

using useFieldContext with an Object

Hey, couldn't find any info on this, is this the correct/a possible way to work with objects? I know I could do name="object.property", however I probably want to be able to change multiple properties of the same object in 1 component

This is my component:
import { useFieldContext } from "@/hooks/form";
import type { HeldTalent } from "@/schemas/held.schema";
import { TableCell, TableRow } from "./ui/table";
import { talentByName } from "@/lib/talente";
import { Input } from "./ui/input";

export default function TalentRow() {
    const field = useFieldContext<HeldTalent>();
    const talent = talentByName(field.state.value.talentName);

    if(!talent) return;

    return (
        <TableRow>
            <TableCell>{talent.name}</TableCell>
            <TableCell>
                <Input
                    type="number"
                    id={field.name}
                    value={field.state.value.TaW}
                    onChange={(e) => field.handleChange({...field.state.value, TaW: e.target.valueAsNumber})}
                >
                </Input>
            </TableCell>
        </TableRow>
    )
}

And this is the Type definition:
export const HeldTalentSchema = z.object({
    talentName:     z.string(),
    TaW:            z.number().int(),
    spEXP:          z.number().int()
});
export type HeldTalent = z.infer<typeof HeldTalentSchema>;
Was this page helpful?