T
TanStack16mo ago
eastern-cyan

how to get the change value of specific field at the root level

How to get the onchanged (like react hook form useWatch hook) value from the useForm for the trigger useEffect in my case.
2 Replies
other-emerald
other-emerald16mo ago
you can use something like that
const form = useForm<{ search: string }>({ defaultValue: { search: '' } });
const search = form.useField({ name: 'search' }).getValue();

useEffect(() => {
// your code here
}, [search]);
const form = useForm<{ search: string }>({ defaultValue: { search: '' } });
const search = form.useField({ name: 'search' }).getValue();

useEffect(() => {
// your code here
}, [search]);
but authors don't reccomend use this way, try use that way
<Field name="search">{field => <Component value={field.state.value} />}</Field>

const Component = ({value}) => {
useEffect(() => {
// your code here
}, [value]);
}
<Field name="search">{field => <Component value={field.state.value} />}</Field>

const Component = ({value}) => {
useEffect(() => {
// your code here
}, [value]);
}
stormy-gold
stormy-gold16mo ago
I'd probably go with const myValue = form.useStore((state) => state.values.myField); and then you can use myValue in your useEffect

Did you find this page helpful?