Skip to content

Commit 1aa1792

Browse files
committed
fix: correct setup context slots and attrs reactivity
1 parent e9e913a commit 1aa1792

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/guide/composition-api-setup.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ The second argument passed to the `setup` function is the `context`. The `contex
5757

5858
export default {
5959
setup(props, context) {
60-
// Attributes (Reactive Property)
60+
// Attributes (Non-reactive object)
6161
console.log(context.attrs)
6262

63-
// Slots (Reactive Property)
63+
// Slots (Non-reactive object)
6464
console.log(context.slots)
6565

6666
// Emit Events (Method)
@@ -69,31 +69,18 @@ export default {
6969
}
7070
```
7171

72-
Because it is a normal JavaScript object, i.e., it is not reactive, this means you can safely use ES6 destructuring on `context`.
72+
The `context` object is a normal JavaScript object, i.e., it is not reactive, this means you can safely use ES6 destructuring on `context`.
7373

7474
```js
7575
// MyBook.vue
7676
export default {
77-
setup(props, { attrs, slots, emit }) {
78-
...
79-
}
77+
setup(props, { attrs, slots, emit }) {
78+
...
79+
}
8080
}
8181
```
8282

83-
As a result, similar to `props`, if you need to destructure either of these properties, you can utilize the `toRefs` method to create a similar effects.
84-
85-
```jsx
86-
// MyBook.vue
87-
88-
import { toRefs } from 'vue'
89-
90-
export default {
91-
setup(props, { attrs }) {
92-
const { id } = toRefs(attrs)
93-
console.log(id.value)
94-
}
95-
)
96-
```
83+
`attrs` and `slots` are stateful objects that are always updated when the component itself is updated. This means you should avoid destructuring them and always reference properties as `attrs.x` or `slots.x`. Also note that unlike `props`, `attrs` and `slots` are **not** reactive. If you intend to apply side effects based on `attrs` or `slots` changes, you should do so inside an `onUpdated` lifecycle hook.
9784

9885
## Accessing Component Properties
9986

0 commit comments

Comments
 (0)