最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - useFormContext()'s watch not triggering a re-render - Stack Overflow

programmeradmin4浏览0评论

In a const component, I have this watch configured:

const { watch, setValue, control } = useFormContext<QuoteCalculatorValues>();
const fuelCostWatch = watch('fees')?.['fuel_costs']?.[0];
const watchPath = `fees.[fuel_costs][0].`;

'fees' is a key/value array, with the following:

export type FormPayableAccessoryFeeDto = {
    quantity: number;
    rate: number;
};

export interface FormPayableAccessoryFeeIndexDto {
    [key: string]: FormPayableAccessoryFeeDto[] | undefined;
}

export type QuoteCalculatorValues = {
    fees: FormPayableAccessoryFeeIndexDto;
};

Here's an input I configured to be monitored by the watch

<Controller
  name={`fees.[fuel_costs][0].quantity`}
  control={control}
  render={({ field }) => (
    <Input
      field={field}
      defaultValue={0}
      onChange={value => {
        setValue(`fees.[fuel_costs][0].quantity`, value as never);
      }}
    />
  )}
/>

I'm expecting that because I'm watch'ing values in fee, when I change the value in the input, the component should re-render.

One thing I noticed, is that in onChange, I have to cast value as never, which might indicate a problem I guess?

I tested this with a more basic structure (i.e. if I watch('info') and create an input on info.firstName) and this works.

Is there a problem with the way my form data's structured?

It works if I configure the watch to listen to the entire form, but I'm worried that might slow things down a bit if arrays get populated with a lot of data (i.e. if I have more indices than fuel_costs).

const fuelCostWatch = watch().fees['fuel_costs']?.[0];

In a const component, I have this watch configured:

const { watch, setValue, control } = useFormContext<QuoteCalculatorValues>();
const fuelCostWatch = watch('fees')?.['fuel_costs']?.[0];
const watchPath = `fees.[fuel_costs][0].`;

'fees' is a key/value array, with the following:

export type FormPayableAccessoryFeeDto = {
    quantity: number;
    rate: number;
};

export interface FormPayableAccessoryFeeIndexDto {
    [key: string]: FormPayableAccessoryFeeDto[] | undefined;
}

export type QuoteCalculatorValues = {
    fees: FormPayableAccessoryFeeIndexDto;
};

Here's an input I configured to be monitored by the watch

<Controller
  name={`fees.[fuel_costs][0].quantity`}
  control={control}
  render={({ field }) => (
    <Input
      field={field}
      defaultValue={0}
      onChange={value => {
        setValue(`fees.[fuel_costs][0].quantity`, value as never);
      }}
    />
  )}
/>

I'm expecting that because I'm watch'ing values in fee, when I change the value in the input, the component should re-render.

One thing I noticed, is that in onChange, I have to cast value as never, which might indicate a problem I guess?

I tested this with a more basic structure (i.e. if I watch('info') and create an input on info.firstName) and this works.

Is there a problem with the way my form data's structured?

It works if I configure the watch to listen to the entire form, but I'm worried that might slow things down a bit if arrays get populated with a lot of data (i.e. if I have more indices than fuel_costs).

const fuelCostWatch = watch().fees['fuel_costs']?.[0];
Share Improve this question edited Mar 10 at 20:54 Drew Reese 204k18 gold badges245 silver badges273 bronze badges asked Mar 10 at 19:47 Francis DucharmeFrancis Ducharme 4,9978 gold badges47 silver badges88 bronze badges 1
  • Please edit your question to include a minimal reproducible example so that readers can run your code to answer your question. Where is the 'const component'? Where is the og useForm declaration? What is '[fuel_costs]'?(is it dynamic?) I can't reproduce this. Anyways, please consider using useFieldArray and useController. – vzsoares Commented Mar 10 at 22:34
Add a comment  | 

1 Answer 1

Reset to default 2

Try watching your property like this

const fuelCostWatch = watch('fees.fuel_costs.0');

Instead of

const fuelCostWatch = watch('fees')?.['fuel_costs']?.[0];
发布评论

评论列表(0)

  1. 暂无评论