Skip to content

Commit e41f441

Browse files
committed
document setup props key presence details
vuejs/core#3889
1 parent 620c04d commit e41f441

File tree

2 files changed

+67
-44
lines changed

2 files changed

+67
-44
lines changed

src/api/composition-api.md

+66-43
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,51 @@
44
55
## `setup`
66

7-
A component option that is executed **before** the component is created, once the `props` are resolved, and serves as the entry point for composition API's
7+
A component option that is executed **before** the component is created, once the `props` are resolved. It serves as the entry point for composition APIs.
88

99
- **Arguments:**
1010

1111
- `{Data} props`
1212
- `{SetupContext} context`
1313

14+
Similar to `this.$props` when using Options API, the `props` object will only contain explicitly declared props. Also, all declared prop keys will be present on the `props` object, regardless of whether it was passed by the parent component or not. Absent optional props will have a value of `undefined`.
15+
16+
If you need to check the absence of an optional prop, you can give it a Symbol as its default value:
17+
18+
```js
19+
const isAbsent = Symbol()
20+
21+
export default {
22+
props: {
23+
foo: { default: isAbsent }
24+
},
25+
setup(props) {
26+
if (props.foo === isAbsent) {
27+
// foo was not provided.
28+
}
29+
}
30+
}
31+
```
32+
1433
- **Typing**:
1534

16-
```ts
17-
interface Data {
18-
[key: string]: unknown
19-
}
35+
```ts
36+
interface Data {
37+
[key: string]: unknown
38+
}
2039

21-
interface SetupContext {
22-
attrs: Data
23-
slots: Slots
24-
emit: (event: string, ...args: unknown[]) => void
25-
}
40+
interface SetupContext {
41+
attrs: Data
42+
slots: Slots
43+
emit: (event: string, ...args: unknown[]) => void
44+
}
2645

27-
function setup(props: Data, context: SetupContext): Data
28-
```
46+
function setup(props: Data, context: SetupContext): Data
47+
```
2948

30-
::: tip
31-
To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed.
32-
:::
49+
::: tip
50+
To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed.
51+
:::
3352

3453
- **Example**
3554

@@ -129,48 +148,52 @@ The component instance context is also set during the synchronous execution of l
129148

130149
- **Typing**:
131150

132-
```ts
133-
interface InjectionKey<T> extends Symbol {}
134-
135-
function provide<T>(key: InjectionKey<T> | string, value: T): void
136-
137-
// without default value
138-
function inject<T>(key: InjectionKey<T> | string): T | undefined
139-
// with default value
140-
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
141-
// with factory
142-
function inject<T>(
143-
key: InjectionKey<T> | string,
144-
defaultValue: () => T,
145-
treatDefaultAsFactory: true
146-
): T
147-
```
151+
```ts
152+
interface InjectionKey<T> extends Symbol {}
153+
154+
function provide<T>(key: InjectionKey<T> | string, value: T): void
155+
156+
// without default value
157+
function inject<T>(key: InjectionKey<T> | string): T | undefined
158+
// with default value
159+
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
160+
// with factory
161+
function inject<T>(
162+
key: InjectionKey<T> | string,
163+
defaultValue: () => T,
164+
treatDefaultAsFactory: true
165+
): T
166+
```
148167

149-
Vue provides an `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer:
168+
Vue provides an `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer:
150169

151-
```ts
152-
import { InjectionKey, provide, inject } from 'vue'
170+
```ts
171+
import { InjectionKey, provide, inject } from 'vue'
153172
154-
const key: InjectionKey<string> = Symbol()
173+
const key: InjectionKey<string> = Symbol()
155174
156-
provide(key, 'foo') // providing non-string value will result in error
175+
provide(key, 'foo') // providing non-string value will result in error
157176
158-
const foo = inject(key) // type of foo: string | undefined
159-
```
177+
const foo = inject(key) // type of foo: string | undefined
178+
```
160179

161-
If using string keys or non-typed symbols, the type of the injected value will need to be explicitly declared:
180+
If using string keys or non-typed symbols, the type of the injected value will need to be explicitly declared:
162181

163-
```ts
164-
const foo = inject<string>('foo') // string | undefined
165-
```
182+
```ts
183+
const foo = inject<string>('foo') // string | undefined
184+
```
166185

167186
- **See also**:
168187
- [Provide / Inject](../guide/component-provide-inject.html)
169188
- [Composition API Provide / Inject](../guide/composition-api-provide-inject.html)
170189

171190
## `getCurrentInstance`
172191

173-
`getCurrentInstance` enables access to an internal component instance useful for advanced usages or for library creators.
192+
`getCurrentInstance` enables access to an internal component instance.
193+
194+
:::warning
195+
`getCurrentInstance` is only exposed for advanced use cases, typically in libraries. Usage of `getCurrentInstance` is strongly discouraged in application code. Do **NOT** use it as an escape hatch to get the equivalent of `this` in Composition API.
196+
:::
174197

175198
```ts
176199
import { getCurrentInstance } from 'vue'

src/guide/composition-api-introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Now that we know the **why** we can get to the **how**. To start working with th
7373

7474
<VideoLesson href="https://www.vuemastery.com/courses/vue-3-essentials/setup-and-reactive-references" title="Learn how setup works with Vue Mastery">Watch a free video on setup on Vue Mastery</VideoLesson>
7575

76-
The new `setup` component option is executed **before** the component is created, once the `props` are resolved, and serves as the entry point for composition API's.
76+
The new `setup` component option is executed **before** the component is created, once the `props` are resolved, and serves as the entry point for composition APIs.
7777

7878
::: warning
7979
You should avoid using `this` inside `setup` as it won't refer to the component instance. `setup` is called before `data` properties, `computed` properties or `methods` are resolved, so they won't be available within `setup`.

0 commit comments

Comments
 (0)