documentation/content/developer/reference/javascript/hooks/hooks.rst
Simon Genin (ges) fd28f72c68 wip
2021-10-12 12:06:37 +02:00

50 lines
811 B
ReStructuredText

Hooks
=====
Overview
--------
* `useBus <#usebus>`_
useBus
------
This hook ensures a bus is properly used by a component:
* each time the component is **mounted** , the callback registers on the bus,
* each time the component is **unmounted** , the callback unregisters off the bus.
API
^^^
..
.. code-block:: ts
useBus(bus: EventBus, eventName: string, callback: Callback): void
where
* ``bus`` is the event bus to use.
* ``eventName`` is the event name to register a callback for.
* ``callback`` gets called when the ``bus`` dispatches an ``eventName`` event.
Example
^^^^^^^
.. code-block:: js
class MyComponent extends Component {
setup() {
useBus(this.env.bus, "some-event", this.myCallback);
}
myCallback() {
// ...
}
}