Skip to content

SetProps on object prop child changes does not trigger computed or watcher #761

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

Closed
drewtownchi opened this issue Jun 26, 2018 · 5 comments
Closed
Labels

Comments

@drewtownchi
Copy link

drewtownchi commented Jun 26, 2018

Version

1.0.0-beta.20

Reproduction link

https://github.com/drewtownchi/VueRouterTranspile

Steps to reproduce

Create some data to be passed down as a prop...

prop1: {
    sub1: "hello",
    sub2: "data2"
}

Create a component that accepts an object prop.
Initialize the test with propsData and verify the child key sub1 is set correctly via the computed property to "hello".
Change the sub1 data in the test to "test2" and update the props using setProps
Check that the sub1 is set correctly via computed property or data (if using a watcher, both don't work)

    expect(wrapper.vm.someData).toBe("hello");
    propsDataForNew.sub1 = "test2";
    wrapper.setProps({
      prop1: propsDataForNew
    });
    expect(wrapper.vm.someData).toBe("test2");

What is expected?

the computed property would be updated to the new data passed down via props

What is actually happening?

The computed property is giving the old data.

Expected: "test2"
Received: "hello"


This is working correctly in the browser. The data outputted to the screen reflects the change in sub1 when sub1 is changed in the parent.

Edit: This was working before in previous versions. So maybe I am just doing it the wrong way now? Now should I be just passing in the updated key?

    wrapper.setProps({
      prop1: {
        sub1: "test2"
      }
    });

This seems to work. Not sure if I should no longer be passing in an updated object?

@drewtownchi
Copy link
Author

drewtownchi commented Jun 27, 2018

The above "working" example doesn't actually work because other properties on the object are unset by calling setProps with the partial object.

So, it seems like I should be passing in the full object to setProps still but it isn't forcing an update on computed/watchers.

It does seem the reactivity does happen if I copy the the initial object with Object.assign or the spread operator and then change the copied object.

@eddyerburgh
Copy link
Member

Yes the problem is you're calling setProps with the same object.

When setProps reassigns the same object, the property getter returns early because the object reference hasn't changed.

I'll look into a way to fix this

@eddyerburgh
Copy link
Member

eddyerburgh commented Jul 5, 2018

The solution has been to throw an error if you're doing this.

Instead, you'll need to create a new object when you call setProps

@CindyConway
Copy link

The above comment says you will need to ' create a new when you call setProps'. A new what. It looks like a word is missing.

@eddyerburgh
Copy link
Member

@CindyConway a new object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants