refactor: remove implicit reactive() call on renderContext

reference: https://github.com/vuejs/rfcs/issues/121

BREAKING CHANGE: object returned from `setup()` are no longer implicitly
passed to `reactive()`.

  The renderContext is the object returned by `setup()` (or a new object
  if no setup() is present). Before this change, it was implicitly passed
  to `reactive()` for ref unwrapping. But this has the side effect of
  unnecessary deep reactive conversion on properties that should not be
  made reactive (e.g. computed return values and injected non-reactive
  objects), and can lead to performance issues.

  This change removes the `reactive()` call and instead performs a
  shallow ref unwrapping at the render proxy level. The breaking part is
  when the user returns an object with a plain property from `setup()`,
  e.g. `return { count: 0 }`, this property will no longer trigger
  updates when mutated by a in-template event handler. Instead, explicit
  refs are required.

  This also means that any objects not explicitly made reactive in
  `setup()` will remain non-reactive. This can be desirable when
  exposing heavy external stateful objects on `this`.
This commit is contained in:
Evan You
2020-01-27 15:15:22 -05:00
parent 763faac182
commit 6b10f0cd1d
3 changed files with 25 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
import { VNode, VNodeChild, isVNode } from './vnode'
import { ReactiveEffect, reactive, shallowReadonly } from '@vue/reactivity'
import { ReactiveEffect, shallowReadonly } from '@vue/reactivity'
import {
PublicInstanceProxyHandlers,
ComponentPublicInstance,
@@ -375,7 +375,7 @@ export function handleSetupResult(
}
// setup returned bindings.
// assuming a render function compiled from template is present.
instance.renderContext = __SSR__ ? setupResult : reactive(setupResult)
instance.renderContext = setupResult
} else if (__DEV__ && setupResult !== undefined) {
warn(
`setup() should return an object. Received: ${
@@ -453,7 +453,7 @@ function finishComponentSetup(
}
if (instance.renderContext === EMPTY_OBJ) {
instance.renderContext = __SSR__ ? {} : reactive({})
instance.renderContext = {}
}
}