Subscribe on changes!

how to validate and reset value in watch?

avatar
May 6th 2022

Version

3.2.31

Reproduction link

sfc.vuejs.org/

Steps to reproduce

if value is object, i want it can use v-model, because computed attr change can not trigger set, i need to use watch to listen the attr change, but user`s some input is valid , i need to reset the value, when i use like this, can not work normally.

interface Props {
  item: Record<string, unknown>;
  modelValue: {
    condition: string;
    value: unknown;
  };
}
const props = defineProps<Props>();
const emit = defineEmits(['update:modelValue']);
const field = toRef(props, 'modelValue');

watch(
  () => field.value,
  val => {
    const data = cloneDeep(val);
    const value = data.value;
    if (Array.isArray(value)) {
      data.value = value.filter(elem => elem !== null);
    }
    emit('update:modelValue', data);
  },
  {
    deep: true
  }
);

What is expected?

work normally

What is actually happening?

Maximum recursive updates exceeded in component . This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself. Possible sources include component template, render function, updated hook or watcher source function.

avatar
May 6th 2022

The Jack can't selected. Example

avatar
May 6th 2022

The Jack can't selected. Example

This is my mistake. i reset it need a condition.

avatar
May 6th 2022

Please close this problem, Thanks.