You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: active-rfcs/0000-function-api.md
+44-2Lines changed: 44 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -121,7 +121,7 @@ Function-based APIs are exposed as named ES exports and imported on demand. This
121
121
122
122
## The `setup` function
123
123
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:
125
125
126
126
```js
127
127
constMyComponent= {
@@ -136,7 +136,49 @@ const MyComponent = {
136
136
137
137
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).
138
138
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
+
constMyComponent= {
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
+
constMyComponent= {
158
+
setup(props, { refs }) {
159
+
// a function that may get called at a later stage
160
+
functiononClick() {
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:
0 commit comments