Skip to content

Hooks running twice on mount when using 19.0 #64780

@tomusborne

Description

@tomusborne

Description

useEffect hooks are running twice on mount when using Gutenberg 19.1.0-rc.1.

For example:

useEffect( () => {
    console.log( 'mounted' );
}, [] );

Without the Gutenberg plugin, this runs one time.

With the Gutenberg plugin, this runs twice.

This is especially an issue when using a pattern like the below.

const isOnMount = useRef( true );

useEffect( () => {
    if ( isOnMount.current ) {
      isOnMount.current = false;
      return; // Skip the first render
    }

    // Code to run on updates only
    console.log( 'useEffect ran on update only' );
}, [ dep ] );

Using Gutenberg, code that is not intended to run on mount is running.

That being said, it's just generally a bad thing that hooks are running twice like this.

Step-by-step reproduction instructions

  1. Activate Gutenberg 19.0
  2. Implement a simple on mount hook with a console.log() in it.
  3. Check the different between your console when using the Gutenberg plugin and not.

Environment info

  • WordPress 6.6.1
  • Gutenberg 19.1.0-rc.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions