Skip to content

Commit

Permalink
docs: update getContext() usage examples with and without namespace a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
dhananjaykuber committed Jul 11, 2024
1 parent 5ca43e8 commit b902755
Showing 1 changed file with 27 additions and 0 deletions.
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

0 comments on commit b902755

Please sign in to comment.