Skip to content

Commit

Permalink
feat(sfc): useAttrs + useSlots
Browse files Browse the repository at this point in the history
Deprecate useContext
  • Loading branch information
yyx990803 committed Jun 23, 2021
1 parent 6f6f0cf commit 63e9e2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,27 @@ export function defineEmits() {
*/
export const defineEmit = defineEmits

/**
* @deprecated use `useSlots` and `useAttrs` instead.
*/
export function useContext(): SetupContext {
if (__DEV__) {
warn(
`\`useContext()\` has been deprecated and will be removed in the ` +
`next minor release. Use \`useSlots()\` and \`useAttrs()\` instead.`
)
}
const i = getCurrentInstance()!
if (__DEV__ && !i) {
warn(`useContext() called without active instance.`)
}
return i.setupContext || (i.setupContext = createSetupContext(i))
}

export function useSlots(): SetupContext['slots'] {
return useContext().slots
}

export function useAttrs(): SetupContext['attrs'] {
return useContext().attrs
}
8 changes: 3 additions & 5 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,11 +815,9 @@ export function finishComponentSetup(
}
}

const attrHandlers: ProxyHandler<Data> = {
const attrDevProxyHandlers: ProxyHandler<Data> = {
get: (target, key: string) => {
if (__DEV__) {
markAttrsAccessed()
}
markAttrsAccessed()
return target[key]
},
set: () => {
Expand Down Expand Up @@ -847,7 +845,7 @@ export function createSetupContext(
// properties (overwrites should not be done in prod)
return Object.freeze({
get attrs() {
return new Proxy(instance.attrs, attrHandlers)
return new Proxy(instance.attrs, attrDevProxyHandlers)
},
get slots() {
return shallowReadonly(instance.slots)
Expand Down

0 comments on commit 63e9e2e

Please sign in to comment.