# onDestroy() doesn't work for kibana canvas

**URL:** <https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449>\
**Category:** Kibana\
**Created:** [November 19, 2019, 7:54am UTC](https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449 "2019-11-19T07:54:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nordask1](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@Nordask1](https://discuss.elastic.co/u/Nordask1)\
**Post date:** [November 19, 2019, 7:54am UTC](https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449/1 "2019-11-19T07:54:46Z")

</div>

Hello. I've created simple plugin for canvas. Kibana version 7.3.  
Something like this:

const browserFunctions = [  
() =\> ({  
name: 'myPlugin',  
fn(data, args) {  
return {  
type: 'render',  
as: 'myPlugin',  
value: {data}  
}  
}  
}),  
];

const elements = [  
() =\> ({  
name: 'myPlugin',  
displayName: 'My plugin',  
image: '',  
expression: 'filters | myPlugin | render',  
}),  
];

const renderers = () =\> ({  
name: 'myComponent',  
reuseDomNode: true,  
render(domNode, data, handlers) {

```
ReactDOM.render(<MyComponent />, domNode, () => handlers.done());

handlers.onDestroy(() => ReactDOM.unmountComponentAtNode(domNode));

```

}  
});;

kbnInterpreter.register({  
elements,  
browserFunctions,  
renderers,  
});

In MyComponent i added method componentWillUnmount(). When i refreshed page with canvas or went to another page unmounting didn't run. Have i made mistake here  
handlers.onDestroy(() =\> ReactDOM.unmountComponentAtNode(domNode)); ?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [November 19, 2019, 10:14am UTC](https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449/2 "2019-11-19T10:14:22Z")

</div>

Hi @Nordask1,

your code looks correct. The `onDestroy` hook is only called when the element is removed from the screen while staying within the Canvas application (e.g. removing it in edit mode or switching canvas sheets).

When navigating away from the page or reloading the browser tears down and recreates the complete Kibana client which does not trigger the `onDestroy` hook.

I'm not 100% sure whether this is intended or not, do you have more insights here @Catherine_Liu @clintandrewhall ?

To make it work now you can reside using the `onunload` event handler on the window object: [https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload)

---

<div class="post-metadata">

**Author:** ![Nordask1](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@Nordask1](https://discuss.elastic.co/u/Nordask1)\
**Post date:** [November 19, 2019, 4:29pm UTC](https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449/3 "2019-11-19T16:29:07Z")

</div>

Thx for answer. I didn't know about recreating kibana client.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 17, 2019, 4:29pm UTC](https://discuss.elastic.co/t/ondestroy-doesnt-work-for-kibana-canvas/208449/4 "2019-12-17T16:29:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
