-
Notifications
You must be signed in to change notification settings - Fork 4.7k
3.2 updates #1157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.2 updates #1157
Changes from all commits
4a95d84
b55bd00
2fe150d
7b25e89
a747c66
8c7cabe
373d3cd
8ff23c7
4573adc
a4cf142
e71274b
d337d21
55505e7
4a37fe0
cf83e29
8614424
ff3e222
2ad63ec
f1a3a27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Effect Scope API <Badge text="3.2+" /> | ||
yyx990803 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:::info | ||
Effect scope is an advanced API primarily intended for library authors. For details on how to leverage this API, please consult its corresponding [RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yyx990803 should we eventually move the RFC to the Guide section of the docs? Definitely not the scope of the current PR but we can create a follow-up after 3.2 is released There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. I want to think about where it should be fit into the doc structure though, maybe a section for "advanced reactivity patterns". |
||
::: | ||
|
||
## `effectScope` | ||
|
||
Creates an effect scope object which can capture the reactive effects (e.g. computed and watchers) created within it so that these effects can be disposed together. | ||
|
||
**Typing:** | ||
|
||
```ts | ||
function effectScope(detached?: boolean): EffectScope | ||
|
||
interface EffectScope { | ||
run<T>(fn: () => T): T | undefined // undefined if scope is inactive | ||
stop(): void | ||
} | ||
``` | ||
|
||
**Example:** | ||
|
||
```js | ||
const scope = effectScope() | ||
|
||
scope.run(() => { | ||
const doubled = computed(() => counter.value * 2) | ||
|
||
watch(doubled, () => console.log(doubled.value)) | ||
|
||
watchEffect(() => console.log('Count: ', doubled.value)) | ||
}) | ||
|
||
// to dispose all effects in the scope | ||
scope.stop() | ||
``` | ||
|
||
## `getCurrentScope` | ||
|
||
Returns the current active [effect scope](#effectscope) if there is one. | ||
|
||
**Typing:** | ||
|
||
```ts | ||
function getCurrentScope(): EffectScope | undefined | ||
``` | ||
|
||
## `onScopeDispose` | ||
|
||
Registers a dispose callback on the current active [effect scope](#effectscope). The callback will be invoked when the associated effect scope is stopped. | ||
|
||
This method can be used as a non-component-coupled replacement of `onUnmounted` in reusable composition functions, since each Vue component's `setup()` function is also invoked in an effect scope. | ||
|
||
**Typing:** | ||
|
||
```ts | ||
function onScopeDispose(fn: () => void): void | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.