#How to type useFieldContext or useFormContext?
60 messages · Page 1 of 1 (latest)
Depends on what you plan to use it for.
useFormContext is a FormApi that could come from anywhere. It'll be available to any form calling it.
- You have access to
Subscribewith form-related state and methods - You have no info on what fields the form may have, as it could be any fields
useFieldContext<T> is a Field context with the inferred value of T. It's available on fields calling it.
- You have access to field state and field-related meta state.
- Errors could have different types depending on what validators were used at runtime. Passing typed errors as prop instead is the recommendation.
Keep the Philosophy section of the docs in mind
i thought i should be able to pass FormSchema to the whole form?
I want to do things like call setFieldValue on other fields
use withForm or withFieldGroup for that instead.
withForm assumes you want to split one form into multiple segments / files. It‘s the most type safe but least reusable
withFieldGroup expects certain fields to be present, so it is more reusable
thank you mama
I still having a hard time chose one over another,
so I have multiple ARRAY FIELD, what should i use? withForm or withFieldGroup?
- How many forms will use this array field?
only 1
use withForm
you have one parameter needed (the index), but you can pass that to it
Say you have foo[${index}]and you'd like to work inside that array in withForm
you'd give it the prop index: number, and then create the namespace of this withForm:
render: function Render({ form, index }) {
const namespace = `foo[${index}]` as const // types it as string literal instead of 'string'
return <form.AppField name={`${namespace}.bar`}>
}
why does it have type of never[]
nvm, cool! It was actually a different problem
weird that the name prop didn't show the error
it usually tells you if you passed it a wrong name
i needed to assert type in defaultValues, otherwise empty strings array will be typed as never
welcome to an irritating gimmick in TypeScript
as isn't type safe, satisfies doesn't change the type
so the true type-safe way would be to use satisfies FormSchema as FormSchema which is not very fun
quick question, can I write useQuery inside function Render() {}?
it's a function component, yes
ESLint might complain, but that is likely because you made it an anonymous function or wrote render in lowercase
also, I can't default export component defined with withForm
what do you mean?
no, actually something else😭
do you think render() should have return type React.ReactNode instead of React.JSX.Element?
i'm trying to return null when there's no data.
if (!data) return null
It's typed as FunctionComponent, so it seems like React wants you to use a JSX element explicitly
simple enough, if (!data) return <></>
already do that. Thank u. Just wonder if it better.
you could use Suspensetoo since it's its own section
assuming with useQuery you're using TanStack Query
yes, I'm using TanStack Query. What you mean by using Suspense for this? not sure if I deeply understand Suspense
check out the useSuspenseQuery docs
useful if you have no reason to have pending states and just want data
Suspense is basically "let the parent component deal with pending state, I don't render until the data exists"
okay, now it's something else
looks like you have cyclic dependencies?
maybe it's something more. Do you use a meta-framework?
yes, nextjs
i move the component to the same file. I don't understand why this happen
what does your usage of Filters look like
so i have mutiple checkboxes, if checked 2025, 2024, 2023 the values will be like
{ published_in: ["2025", "2024", "2023"] }
the error implies form is undefined. I mean how is the form created and how is it passed to Filters
i tried to see what inside but form (props) was an empty object.
it should look like <Filters form={form} />. I assume you didn't do that, then?
oh sh*t, I did not
it does. I'm just blind.
@neon skiff everytime I import withForm in another component file I get this error.
okay so I was
export { withForm } = createHookForm() in my page.tsx
import { withForm } from "./page.tsx" in my filter.tsx
then
export default Filters in my filter.tsx
import Filters from "filters.tsx" in my page.tsx
🫣 😭
i moved to createHookForm to a seperate file. then it fixed
so that then
what is the point of defining fieldComponents and formComponents
in createFormHook when I can wrap any of that component with withForm ?