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

shallowReactive inside a reactive object is not respected when the property is reassigned #5271

Closed
sqal opened this issue Jan 16, 2022 · 1 comment

Comments

@sqal
Copy link
Contributor

sqal commented Jan 16, 2022

Version

3.2.26

Reproduction link

SFC Playground

Steps to reproduce

  • click add
  • click reset
  • click add

What is expected?

I expect the values of the state.data array to always be non-reactive

What is actually happening?

Values inside the array are not reactive only the first time. When a new shallow reactive array is assigned, then its values become reactive.

@lidlanca
Copy link
Contributor

lidlanca commented Jan 17, 2022

when initialized reactive({...}) state, preserved the shallow proxy

  const getData = () => shallowReactive([{ id: 1 }])
  const state = reactive({ data: getData() })

however the data property is part of a reactive object, and when you set that property
with a new shallowReactive object, vue removed the shallow proxy, and made it part of the reactive object.

your expectation to respect shallowReactive, will actually violate the reactive object principles
where *everything is suppose to be deeply reactive. with some exceptions for example objects marked as raw

my recommendation will be not to mix the two.
but if you really need this use case, you can "protect" it with a ref

  const getData = () => shallowRef(shallowReactive([{ id: 1 }]))
  const state = reactive({ data: getData() })

@github-actions github-actions bot locked and limited conversation to collaborators Oct 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants