the best way to set up an event listener in backdraftjs?

12 Views Asked by At

I want to set up an event listener in a component that will go away when the component is destroyed. Is this the correct way to do that?

this.ownWhileRendered(connect(document, 'click', this.handleDocClick.bind(this)));

connect -> returns a destroyable (https://backdraftjs.org/docs.html#bd-core.functions.connect)

ownWhileRendered -> Ensures Destroyable instances are destroyed when the instance is unrendered (https://backdraftjs.org/docs.html#bd-core.classes.Component.ownWhileRendered)

So does that seem correct?

1

There are 1 best solutions below

0
On

If you wish to make the listener go away:

when the component is destroyed, then

this.own(
    connect(document, 'click', this.handleDocClick.bind(this)));

when the component is unrendered, then

this.ownWhileRendered(
    connect(document, 'click', this.handleDocClick.bind(this)));