From 37406ec7be15404c2d83dd9375d3df825e07a264 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 3 Aug 2022 13:16:04 +0400 Subject: [PATCH] useGlobalStylesOutput: Use memo for derived values --- .../global-styles/use-global-styles-output.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/use-global-styles-output.js b/packages/edit-site/src/components/global-styles/use-global-styles-output.js index c4e9d000b703e..afdebc069d8b5 100644 --- a/packages/edit-site/src/components/global-styles/use-global-styles-output.js +++ b/packages/edit-site/src/components/global-styles/use-global-styles-output.js @@ -22,7 +22,7 @@ import { getBlockTypes, } from '@wordpress/blocks'; import { useSelect } from '@wordpress/data'; -import { useEffect, useState, useContext } from '@wordpress/element'; +import { useContext, useMemo } from '@wordpress/element'; import { getCSSRules } from '@wordpress/style-engine'; import { __unstablePresetDuotoneFilter as PresetDuotoneFilter, @@ -782,9 +782,6 @@ export const getBlockSelectors = ( blockTypes ) => { }; export function useGlobalStylesOutput() { - const [ stylesheets, setStylesheets ] = useState( [] ); - const [ settings, setSettings ] = useState( {} ); - const [ svgFilters, setSvgFilters ] = useState( {} ); const { merged: mergedConfig } = useContext( GlobalStylesContext ); const [ blockGap ] = useSetting( 'spacing.blockGap' ); const hasBlockGapSupport = blockGap !== null; @@ -794,9 +791,9 @@ export function useGlobalStylesOutput() { return !! getSettings().disableLayoutStyles; } ); - useEffect( () => { + return useMemo( () => { if ( ! mergedConfig?.styles || ! mergedConfig?.settings ) { - return; + return []; } const blockSelectors = getBlockSelectors( getBlockTypes() ); @@ -812,7 +809,7 @@ export function useGlobalStylesOutput() { disableLayoutStyles ); const filters = toSvgFilters( mergedConfig, blockSelectors ); - setStylesheets( [ + const stylesheets = [ { css: customProperties, isGlobalStyles: true, @@ -821,15 +818,18 @@ export function useGlobalStylesOutput() { css: globalStyles, isGlobalStyles: true, }, - ] ); - setSettings( mergedConfig.settings ); - setSvgFilters( filters ); + ]; + + return [ + stylesheets, + mergedConfig.settings, + filters, + hasBlockGapSupport, + ]; }, [ hasBlockGapSupport, hasFallbackGapSupport, mergedConfig, disableLayoutStyles, ] ); - - return [ stylesheets, settings, svgFilters, hasBlockGapSupport ]; }