How to get current post id in Gutenberg Block save function?

Question

I have a block that save post ID as attribute. The block is working but the console show me an error reporting "Content generated by save function:" is different from "Content retrieved from post body:".

The problem is that the post ID returns null at first glance and then retrieve the correct ID.

This is my code:

const { getCurrentPostId } = wp.data.select("core/editor");

export default function save( { attributes, className, props } ) {
    const post_id = getCurrentPostId();
    return (
        <form class={ className }>
            <input name="_form_id" type="hidden" value={ post_id } />
            <div style="display: none;">
                <input type="text" name={ '_secret_h' + post_id } value="" />
            </div>
            <InnerBlocks.Content />
        </form>
    );
}

How can I be sure that the ID is correctly retrieved?

0
Francesco Tropicalista 8 months 2020-12-09T22:10:38-05:00 0 Answers 7 views 0

Leave an answer

Browse
Browse