Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: refactor ContextSystemProvider and the useUpdateEffect util to ignore exhaustive-deps #45044

Merged
merged 9 commits into from
Oct 21, 2022
3 changes: 2 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
### Internal

- `AnglePickerControl`: Set Storybook Label control type to 'text' ([#45122](https://github.com/WordPress/gutenberg/pull/45122)).
- `SlotFill`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44403](https://github.com/WordPress/gutenberg/pull/44403))
- `Context`: updated to ignore `react/exhaustive-deps` eslint rule ([#45044](https://github.com/WordPress/gutenberg/pull/45044))
- `Button`: Refactor Storybook to controls and align docs ([#44105](https://github.com/WordPress/gutenberg/pull/44105)).

## 21.3.0 (2022-10-19)
Expand All @@ -39,7 +41,6 @@
- `withFocusReturn`: Refactor tests to `@testing-library/react` ([#45012](https://github.com/WordPress/gutenberg/pull/45012)).
- `ToolsPanel`: updated to satisfy `react/exhaustive-deps` eslint rule ([#45028](https://github.com/WordPress/gutenberg/pull/45028))
- `Tooltip`: updated to ignore `react/exhaustive-deps` eslint rule ([#45043](https://github.com/WordPress/gutenberg/pull/45043))
- `SlotFill`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44403](https://github.com/WordPress/gutenberg/pull/44403))

## 21.2.0 (2022-10-05)

Expand Down
23 changes: 5 additions & 18 deletions packages/components/src/ui/context/context-system-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,21 @@ import {
createContext,
useContext,
useRef,
useEffect,
useMemo,
memo,
} from '@wordpress/element';
import warn from '@wordpress/warning';

/**
ciampo marked this conversation as resolved.
Show resolved Hide resolved
* Internal dependencies
*/
import { useUpdateEffect } from '../../utils';

export const ComponentsContext = createContext(
/** @type {Record<string, any>} */ ( {} )
);
export const useComponentsContext = () => useContext( ComponentsContext );

/**
* Runs an effect only on update (i.e., ignores the first render)
*
* @param {import('react').EffectCallback} effect
* @param {import('react').DependencyList} deps
*/
function useUpdateEffect( effect, deps ) {
const mounted = useRef( false );
useEffect( () => {
if ( mounted.current ) {
return effect();
}
mounted.current = true;
return undefined;
}, deps );
}

/**
* Consolidates incoming ContextSystem values with a (potential) parent ContextSystem value.
*
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/utils/hooks/use-update-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import { useRef, useEffect } from '@wordpress/element';
*/
function useUpdateEffect( effect, deps ) {
const mounted = useRef( false );

useEffect( () => {
if ( mounted.current ) {
return effect();
}
mounted.current = true;
return undefined;
// Disable reasons:
// 1. This hook needs to pass a dep list that isn't an array literal
// 2. `effect` is missing from the array, and will need to be added carefully to avoid additional warnings
// see https://github.com/WordPress/gutenberg/pull/41166
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps );
}

Expand Down