E60B provide context and disable `this` in setup() · vuejs/rfcs@08ad9e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08ad9e1

Browse files
committed
provide context and disable this in setup()
1 parent adefea6 commit 08ad9e1

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

active-rfcs/0000-function-api.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Function-based APIs are exposed as named ES exports and imported on demand. This
121121

122122
## The `setup` function
123123

124-
A new component option, `setup()` is introduced. As the name suggests, this is the place where we use the function-based APIs to setup the logic of our component. `setup()` is called when an instance of the component is created, after props resolution. The function receives the resolved props as its argument:
124+
A new component option, `setup()` is introduced. As the name suggests, this is the place where we use the function-based APIs to setup the logic of our component. `setup()` is called when an instance of the component is created, after props resolution. The function receives the resolved props as its first argument:
125125

126126
``` js
127127
const MyComponent = {
@@ -136,7 +136,49 @@ const MyComponent = {
136136

137137
Note this `props` object is reactive - i.e. it is updated when new props are passed in, and can be observed and reacted upon using the `watch` function introduced later in this RFC. However, for userland code, it is immutable during development (will emit warning if user code attempts to mutate it).
138138

139-
`this` is usable inside `setup()`, but you most likely won't need it very often.
139+
The second argument provides a context object which exposes a number of properties that were previously exposed on `this` in 2.x APIs:
140+
141+
``` js
142+
const MyComponent = {
143+
setup(props, context) {
144+
context.attrs
145+
context.slots
146+
context.refs
147+
context.emit
148+
context.parent
149+
context.root
150+
}
151+
}
152+
```
153+
154+
`attrs`, `slots` and `refs` are in fact proxies to the corresponding values on the internal component instance. This ensures they always expose the latest values even after updates, so we can destructure them without worrying accessing a stale reference:
155+
156+
``` js
157+
const MyComponent = {
158+
setup(props, { refs }) {
159+
// a function that may get called at a later stage
160+
function onClick() {
161+
refs.foo // guaranteed to be the latest reference
162+
}
163+
}
164+
}
165+
```
166+
167+
Why don't we expose `props` via context as well, so that `setup()` needs just a single argument? There are several reasons for this:
168+
169+
- It's much more common for a component to use `props` than the other properties, and very often a component uses only `props`.
170+
171+
- Having `props` as a separate argument makes it easier to type it individually (see [TypeScript-only Props Typing](#typescript-only-props-typing) below) without messing up the types of other properties on the context. It also makes it possible to keep a consistent signature across `setup`, `render` and plain functional components with TSX support.
172+
173+
**`this` is not available inside `setup()`.** The reason for avoiding `this` is because of a very common pitfall for beginners:
174+
175+
``` js
176+
setup() {
177+
function onClick() {
178+
this // not the `this` you'd expect!
179+
}
180+
}
181+
```
140182

141183
## State
142184

0 commit comments

Comments
 (0)
0