Search code examples
javascriptreactjsdojo2

Dojo 2 Is there any after render life cycle hook in dojo2?


I working on Dojo 2 project. I know there are not many projects going on Dojo 2 it's difficult to get support for it. Anyway I am looking for dojo 2 widget after render life cycle hook? In React we have:-

componentDidMount: function() { console.log('Component rendered')},

What about dojo 2 after widget render life cycle hook?


Solution

  • The runAfterRenders method isn't intended to be overridden (and should actually be private) in a widget. Using the @afterRender decorator is the correct hook to introspect the the results of a widgets render as per the readme.

    class MyWidget extends WidgetBase {
    
        @afterRender()
        myAfterRender(vnode: VNode) {
             // do something with the resulting vnode
             vnode.children = [ ...vnode.children, 'Another Text Node' ];
    
             return vnode;
        }
    
    
        protected render() {
            return v('div', [ 'text' ]);
        }
    }
    

    The onAttach hook is probably a more equivalent lifecycle to reacts componentDidMount, this is a method lifecycle that can be implemented in the same way as componentDidMount.

    Hope this helps!