Skip to content

Commit

Permalink
Components: Fix the 'useUpdateEffect' hook in strict mode (WordPress#…
Browse files Browse the repository at this point in the history
…62974)

* Components: Fix the 'useUpdateEffect' hook in strict mode
* Add changelong entry

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: andrewserong <[email protected]>
Co-authored-by: talldan <[email protected]>
  • Loading branch information
4 people authored Jul 1, 2024
1 parent fced091 commit aa304b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- `UnitControl`: Fix colors when disabled. ([#62970](https://github.com/WordPress/gutenberg/pull/62970))
- `useUpdateEffect`: Correctly track mounted state in strict mode. ([#62974](https://github.com/WordPress/gutenberg/pull/62974))

### Internal

Expand Down
9 changes: 8 additions & 1 deletion packages/components/src/utils/hooks/use-update-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useRef, useEffect } from '@wordpress/element';
/**
* A `React.useEffect` that will not run on the first render.
* Source:
* https://github.com/ariakit/ariakit/blob/reakit/packages/reakit-utils/src/useUpdateEffect.ts
* https://github.com/ariakit/ariakit/blob/main/packages/ariakit-react-core/src/utils/hooks.ts
*
* @param {import('react').EffectCallback} effect
* @param {import('react').DependencyList} deps
Expand All @@ -25,6 +25,13 @@ function useUpdateEffect( effect, deps ) {
// see https://github.com/WordPress/gutenberg/pull/41166
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps );

useEffect(
() => () => {
mounted.current = false;
},
[]
);
}

export default useUpdateEffect;

0 comments on commit aa304b2

Please sign in to comment.