Gutenberg: Event or callback after block loaded on editor

Created on 2 Aug 2018  路  10Comments  路  Source: WordPress/gutenberg

I'm making a block that using ServerSideRender. In this case, i will need to trigger some javascript (jquery) function when the element finish loaded on the backend editor. Is there any event listener or hook for gutenberg for this case?

Needs Technical Feedback [Package] Server Side Render [Type] Help Request

Most helpful comment

I was also looking for the same options but could not find any solutions anywhere. It seems like no event trigger after block loaded.

I have created following function and it works.

let blockLoaded = false;
let blockLoadedInterval = setInterval(function() {
    if (document.getElementById('post-title-0')) {/*post-title-0 is ID of Post Title Textarea*/
        //Actual functions goes here

        blockLoaded = true;
    }
    if ( blockLoaded ) {
        clearInterval( blockLoadedInterval );
    }
}, 500);

All 10 comments

@adekherry have you found the solution?

el( ServerSideRender, {
    block: 'meetongo/meetup-slider',
    attributes: props.attributes,
    onChange: sliderIsUpdated()
} ),

I have tried onChange but its triggered before render or right after block settings are changed.

@buzzclue, previously i create a new components (extends the ServerSideRender components), then i override componentDidUpdate method. It's looks like this:

componentDidUpdate( prevProps, prevState ) {
        super.componentDidUpdate(prevProps);
        if ( this.state.response !== prevState.response ) {
            console.log('hit here')
        }
    }

@adekherry I am closing this issue because it appears that you have found a solution already. If I have misread that or if you are suggesting a change to componentDidUpdate would you please leave a comment letting me know so I can re-open the issue in that case?

Hi, I'm still a beginner in React and learning about all the new stuff about WP editor.

I searched a lot but didn't find any related issues other than this, so I chose to reopen it instead of creating another one.

I'm trying to create a custom image slider block/carousel, and I faced the same problem: I need an event to hook the slider init function to.

Whether the block renders the structure directly or using ServerSideRender for doing this, I think it's helpful and very important to have a built-in block-specific event that is fired directly after the block is loaded (each block has its own event, maybe using block name as an identifier).

For example, I used to create custom widgets for Elementor, it provides such an event for every widget so I can make sure that my custom scripts initialized at the right moment.

If there's already such a way for doing this, or I'm missing anything, please direct me.

I also need this.

I have a block that runs some vanilla JS (i.e. not React) as soon as the page is ready. At the moment it's contained inside a jQuery ready block.

jQuery(document).ready(function ($) {
  // code here...
}

This works fine on the front end but not for the block editor as the code should run as soon as the page/editor is ready.

Incidentally, if the code was waiting for some interaction from the user such as a form submission or a mouse-click then it would work fine as the editor would have fully loaded before this could take place.

Ideally, the code should run immediately as soon as the editor is loaded and all blocks have been mounted.

@designsimply Any news on this? I want to call some functions as soon editor is visible and whole page is rendered. On WP 5.2.2, jQuery(document).ready(function () { and wp.domReady(function() { both called the function before components are rendered.

Are there any similar function or events that we can use to accomplish this situation?

Thanks

@mohsinrasool I'm sorry but this is a bit outside of my expertise. May I ask that you please try asking for help at https://wordpress.stackexchange.com/ and include a code sample for them if possible.

I was also looking for the same options but could not find any solutions anywhere. It seems like no event trigger after block loaded.

I have created following function and it works.

let blockLoaded = false;
let blockLoadedInterval = setInterval(function() {
    if (document.getElementById('post-title-0')) {/*post-title-0 is ID of Post Title Textarea*/
        //Actual functions goes here

        blockLoaded = true;
    }
    if ( blockLoaded ) {
        clearInterval( blockLoadedInterval );
    }
}, 500);

It will work in some cases only. Because, React render each component based on its states. So, what I want to do, this snippet can't help me. Thanks for sharing, anyway.

I was also looking for the same options but could not find any solutions anywhere. It seems like no event trigger after block loaded.

I have created following function and it works.

let blockLoaded = false;
let blockLoadedInterval = setInterval(function() {
    if (document.getElementById('post-title-0')) {/*post-title-0 is ID of Post Title Textarea*/
        //Actual functions goes here

        blockLoaded = true;
    }
    if ( blockLoaded ) {
        clearInterval( blockLoadedInterval );
    }
}, 500);

Thank you so much for this snippet. Definitely saved the day. Great workaround.

Although I'm still dissatisfied... It seems there's no way to attach code after all the Gutenberg editor is loaded, being "outside" of Gutenberg. (I wanted to assign 'featured image url' as a background of a styled post_title block)

All solutions I got on google point to "componentDidMount", but I can't access to that unless I'm coding a Gutenberg React plugin I understand... which is not the case.

I need to continue studying.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pfefferle picture pfefferle  路  3Comments

ellatrix picture ellatrix  路  3Comments

youknowriad picture youknowriad  路  3Comments

cr101 picture cr101  路  3Comments

spocke picture spocke  路  3Comments