Difference between useEntityProp and getEditedPostAttribute

[ad_1]

When developing a custom sidebar for the block editor, a panel in the InspectorControls or a block, there are multiple ways to get the properties/fields for the post:

1. getEditedPostAttribute
According to its doc block it “Returns a single attribute of the post being edited, preferring the unsaved edit if one exists, but falling back to the attribute for the last known saved state of the post.”
When using it like getEditedPostAttribute("meta") it will get the whole meta, but it’s possible to also get other properties. Eg the featured image selector in the sidebar gets the attribute featured_media.

2. useEntityProp
According to its doc block it “Hook that returns the value and a setter for the specified property of the nearest provided entity of the specified type.”
In the metabox to block guide it’s recommended to be used to get the meta like `useEntityProp(“postType”, postType, “meta”)”

I understand, that the second one is a react hook and the first one would have to be used in a useSelect select mapper.

But still, what exactly is the difference between these methods?
What is the preferred way and are there cases in which they don’t return the same data? As far as I can see, both return the current (maybe unsaved) state of the object.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer