Skip to content

Conversation

@obenland
Copy link
Member

Fixes React error for undefined dependencies in the useEffect hook based on a race condition between the users and currentUser selectors.

Proposed changes:

  • Refactors useUserOptions to better handle loading state and fetching of users with ActivityPub capability.
  • Now uses isLoadingUsers to avoid fetching current user prematurely, and ensures capability checks only run when appropriate.

Testing instructions:

  • Go to the Editor
  • Insert a Follow Me block and set the user to yourself, save the post.
  • Reload the Editor and make sure the blocks don't error.

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Added - for new features
  • Changed - for changes in existing functionality
  • Deprecated - for soon-to-be removed features
  • Removed - for now removed features
  • Fixed - for any bug fixes
  • Security - in case of vulnerabilities

Message

Blocks relying on user selectors no longer error due to a race condition when fetching users.

Refactors useUserOptions to better handle loading state and fetching of users with ActivityPub capability. Now uses isLoadingUsers to avoid fetching current user prematurely, and ensures capability checks only run when appropriate. Updates block JS bundles to use the new logic.
@obenland obenland requested a review from pfefferle August 27, 2025 18:01
@obenland obenland self-assigned this Aug 27, 2025
Copilot AI review requested due to automatic review settings August 27, 2025 18:01
@github-actions github-actions bot added the [Focus] Editor Changes to the ActivityPub experience in the block editor label Aug 27, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the useUserOptions hook to fix a React error caused by undefined dependencies in a useEffect hook due to a race condition between user selectors and loading states.

  • Introduces proper loading state handling to prevent premature fetching of user data
  • Replaces conditional useSelect patterns with a single useSelect that returns both data and loading state
  • Updates dependency arrays and condition checks to handle loading states appropriately

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/shared/use-user-options.js Refactors useUserOptions hook to properly handle loading states and prevent race conditions
build/followers/index.js Compiled output reflecting the useUserOptions changes
build/followers/index.asset.php Updated asset version hash
build/follow-me/index.js Compiled output reflecting the useUserOptions changes
build/follow-me/index.asset.php Updated asset version hash

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

*/
return useMemo( () => {
if ( ! users ) {
if ( ! users.length ) {
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

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

Potential runtime error: if users is null (when fetchedUsers is null and no fallback user exists), calling .length will throw an error. The condition should check for both null and empty array: if ( ! users || ! users.length )

Suggested change
if ( ! users.length ) {
if ( ! users || ! users.length ) {

Copilot uses AI. Check for mistakes.
@obenland obenland merged commit 2bea435 into trunk Aug 27, 2025
13 checks passed
@obenland obenland deleted the fix/undefined-deps-error branch August 27, 2025 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Focus] Editor Changes to the ActivityPub experience in the block editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants