Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected watch calling for some props with default generating value #3471

Closed
miyanokomiya opened this issue Mar 24, 2021 · 1 comment · Fixed by #3474
Closed

Unexpected watch calling for some props with default generating value #3471

miyanokomiya opened this issue Mar 24, 2021 · 1 comment · Fixed by #3474
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working

Comments

@miyanokomiya
Copy link

miyanokomiya commented Mar 24, 2021

Version

3.0.7

Reproduction link

https://jsfiddle.net/r328ezot/10/

Steps to reproduce

A child component has these options.

  props: {
    foo: {
      type: Object,
      default: () => ({ val: 1 })
    },
    bar: {
      type: Number,
      default: 0
    },
  },
  setup(props) {
    const count = Vue.ref(0)
    Vue.watch(() => props.foo, () => {
      count.value++
    })
    return { count }
  }

The parent has this template and setup function.

<child :foo="foo" :bar="bar"></child>
<button @click="addBar">Add</button>
setup() {
const foo = Vue.ref()
  const bar = Vue.ref(0)
  function addBar() { bar.value++ }
  return { foo, bar, addBar }
}

When I click 'Add' button to change only bar, the child state 'count' increases: watch callback for prop `foo' is triggered.

If the parent does not pass the prop foo, it is not triggered.

<child :bar="bar"></child>

Is it expected behavior?

What is expected?

Watch callback for foo is not triggered if the value is not changed.

What is actually happening?

Watch callback for foo is triggered if the value is not changed.

@edison1105
Copy link
Member

edison1105 commented Mar 24, 2021

as a workround:

  const defaultV = { val: 1 }

  props: {
      foo: {
        type: Object,
        default: () => (defaultV)
      },

    }

@HcySunYang HcySunYang added the 🐞 bug Something isn't working label Mar 24, 2021
@HcySunYang HcySunYang added the 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. label Mar 24, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working
Projects
None yet
3 participants