Skip to content

Conversation

@t-hamano
Copy link
Contributor

@t-hamano t-hamano commented Jun 15, 2023

Fixes: #51466
Related to #39288

What?

This PR fixes an error that occurs when { enabled: false } is specified in the fourth argument of useEntityRecord.

For example, when you write code like this:

const { record, isResolving } = useEntityRecord( 'postType', 'page', 1, {
	enabled: false,
} );

The following error is logged in the browser console:

use-entity-record.ts:182 Uncaught TypeError: Cannot destructure property 'data' of '(0 , _use_query_select__WEBPACK_IMPORTED_MODULE_4__.default)(...)' as it is null.

Why?

This is because if enabed is false, the internal useQuerySelect returns null and the data property cannot be destructured.

How?

I referred to getEntityRecords implementation, but I think it should be the value of the data property that is null, not the entire object.

Testing Instructions

Confirm that the following code does not cause an error.

The record variable should be the post object if the post exists, or undefined if it does not:

const { record } = useEntityRecord( 'postType', 'page', 2 );
const { record } = useEntityRecord( 'postType', 'page', 2, {
	enabled: true,
} );

The record variable should always be null:

const { record } = useEntityRecord( 'postType', 'page', 2, {
	enabled: false,
} );

@t-hamano t-hamano self-assigned this Jun 15, 2023
@t-hamano t-hamano added the [Package] Core data /packages/core-data label Jun 15, 2023
@t-hamano t-hamano marked this pull request as ready for review June 15, 2023 13:09
@t-hamano t-hamano requested a review from nerrad as a code owner June 15, 2023 13:09
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @t-hamano!

@t-hamano t-hamano merged commit b3ab825 into trunk Jun 16, 2023
@t-hamano t-hamano deleted the fix/use-entity-record-destructure branch June 16, 2023 06:59
@github-actions github-actions bot added this to the Gutenberg 16.1 milestone Jun 16, 2023
sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
…e` (WordPress#51534)

* useEntityRecord: Fix destructure error when `enabled` option is `false`

* Delete comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Core data /packages/core-data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The enabled option triggers an error on useEntityRecord if set to false.

3 participants