Skip to content
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

Update getContext() usage examples with namespace argument #63411

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/reference-guides/interactivity-api/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,33 @@ store( "myPlugin", {
});
```

When `getContext()` called with a namespace argument, `getContext(namespace)` retrieves the context specific to that namespace within the current element's context.

```php
// render.php
<div data-wp-interactive="myPlugin" data-wp-context='{ "isOpen": false, "region": { "name": "North" } }'>
<button data-wp-on--click="actions.log">Log</button>
</div>
```

```js
// store
import { store, getContext } from '@wordpress/interactivity';

store( "myPlugin", {
actions: {
log: () => {
// Retrieve the context for the 'region' namespace.
const regionContext = getContext('region');
// Logs "North"
console.log('regionContext => ', regionContext.name);
// Logs the entire 'region' context object.
console.log('regionContext => ', regionContext);
},
},
});
```

#### getElement()

Retrieves a representation of the element that the action is bound to or called from. Such representation is read-only, and contains a reference to the DOM element, its props and a local reactive state.
Expand Down
Loading