-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Interactivity API Docs: Add withScope description #59542
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I left a few suggestions but this looks good.
@@ -1042,6 +1042,30 @@ The code will log: | |||
} | |||
``` | |||
|
|||
### useScope() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### useScope() | |
### withScope() |
### useScope() | ||
|
||
Actions can depend on the scope when they are called, e.g., when you call `getContext()` or `getElement()`. | ||
When the Interactivity API runtime execute callbacks, the scope is set automatically. However, if you call an action from a callback that is not executed by the runtime, like `setInterval()`, you need to ensure that callback sets the scope. The way of doing so is by wrapping the callback with `withScope()` function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the Interactivity API runtime execute callbacks, the scope is set automatically. However, if you call an action from a callback that is not executed by the runtime, like `setInterval()`, you need to ensure that callback sets the scope. The way of doing so is by wrapping the callback with `withScope()` function. | |
When the Interactivity API runtime execute callbacks, the scope is set automatically. However, if you call an action from a callback that is not executed by the runtime, like in a `setInterval()` callback, you need to ensure that the scope is properly set. Use the `withScope()` function to ensure the scope is properly set in these cases. |
const ctx = getContext(); | ||
if ( ctx.autoplay ) { | ||
setInterval( | ||
withScope( () => { | ||
actions.nextImage(); | ||
} ), | ||
state.transitionsSpeed | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove some of the less relevant parts of the example:
const ctx = getContext(); | |
if ( ctx.autoplay ) { | |
setInterval( | |
withScope( () => { | |
actions.nextImage(); | |
} ), | |
state.transitionsSpeed | |
); | |
} | |
setInterval( | |
withScope( () => { | |
actions.nextImage(); | |
} ), | |
3_000 | |
); |
What?
Add
withScope()
function description and an example.