-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addEventListener 'load' not working on FSE editor. #39114
Comments
I think a window object would be appropriate for attaching the load event. And I think useRefEffect would be a good choice based on Make WordPress Core article. Like this: import { useRefEffect } from '@wordpress/compose';
import { useBlockProps } from '@wordpress/block-editor';
export default function Edit() {
const ref = useRefEffect( ( element ) => {
function someFunc() {
// do something...
}
const { ownerDocument } = element;
// Relative window object
const { defaultView } = ownerDocument;
defaultView.addEventListener( 'load', someFunc );
return () => {
// remove event listener.
defaultView.removeEventListener( 'load', someFunc );
};
}, [] );
const blockProps = useBlockProps( { ref } );
return (
<p { ...blockProps }>test</p>
);
} |
@t-hamano thank you for this, but is there some difference between standard editor and FSE editor? and about window listeners, my case is not use window.addEventListener it fire event on ref. |
@grogou |
@t-hamano thank you it is lot of help. |
|
Perhaps the event is not attached because the document is already loaded in the site editor (iframe editor instance). I think it would be better to do the processing directly within import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';
import { useBlockProps } from '@wordpress/block-editor';
export default function Edit() {
const ref = useRef( null );
useEffect( () => {
const { ownerDocument } = ref.current;
// should show "complete", this means that the document has been loaded.
console.log( ownerDocument.readyState );
// Do something directly without using event listener.
}, [] )
const blockProps = useBlockProps({ ref });
return (
<p { ...blockProps }>
{ __( 'Test Block – hello from the editor!', 'test-block' ) }
</p>
);
} |
Curious if @ryanwelcher has any insights here! |
@annezazu anyway this is not working in my side, so if you can suggest anything for this I will by glad for that. Thank you! |
Description
ref.current.addEventListener('load')
not working on FSE editor, but it is working on standard editor.Nothing on console, just listener don't fire.
Step-by-step reproduction instructions
load
listener on someref
Screenshots, screen recording, code snippet
No response
Environment info
Wordpress 5.9.1
Gutenberg 12.6.1
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: