-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Social Icons: Fix effect dependencies #69824
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
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +16 B (0%) Total Size: 1.84 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s nice to see the updater function leveraged 👍. I tested and it works as expected.
Aside: I question if `backgroundBackupRef` is necessary. The effect should only run when `logosOnly` changes so it seems a backup could be closed over by a cleanup function.
diff --git a/packages/block-library/src/social-links/edit.js b/packages/block-library/src/social-links/edit.js
index 4d0b522cbb..7e83807389 100644
--- a/packages/block-library/src/social-links/edit.js
+++ b/packages/block-library/src/social-links/edit.js
@@ -6,7 +6,7 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
-import { useEffect, useRef } from '@wordpress/element';
+import { useEffect } from '@wordpress/element';
import {
BlockControls,
useInnerBlocksProps,
@@ -77,11 +77,11 @@ export function SocialLinksEdit( props ) {
// Remove icon background color when logos only style is selected or
// restore it when any other style is selected.
- const backgroundBackupRef = useRef( {} );
useEffect( () => {
if ( logosOnly ) {
+ let restore;
setAttributes( ( prev ) => {
- backgroundBackupRef.current = {
+ restore = {
iconBackgroundColor: prev.iconBackgroundColor,
iconBackgroundColorValue: prev.iconBackgroundColorValue,
customIconBackgroundColor: prev.customIconBackgroundColor,
@@ -92,8 +92,7 @@ export function SocialLinksEdit( props ) {
customIconBackgroundColor: undefined,
};
} );
- } else {
- setAttributes( { ...backgroundBackupRef.current } );
+ return () => setAttributes( restore );
}
}, [ logosOnly, setAttributes ] );
Another aside: I’m not concerned about it but I’ll note that this backup state can be lost due to remounts. For instance, if you change to logosOnly
and then toggle “Show template” then switch away from logosOnly
.
e2cf899
to
6be0402
Compare
Thanks, @stokesman! I like the idea of using the cleaup method for restoration; it feels more React-y. 😄
This can't be avoided. I think the current logic is only there to prevent color loss while the user is just experimenting and switching styles. |
Flaky tests detected in 6be0402. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/14302551062
|
* Social Icons: Fix effect dependencies * Use local variable Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: stokesman <presstoke@git.wordpress.org> Co-authored-by: getdave <get_dave@git.wordpress.org>
What?
Closes #64896.
PR fixes missing dependencies
useEffect
ESLint warning for Social Icons block.How?
Use the new updater function for
setAttributes
to store previous attribute values. This removes the need to list attributes as dependencies, which could cause triggering side effects unintentionally.Testing Instructions
Testing Instructions for Keyboard
Same.
Screenshots or screencast
CleanShot.2025-04-04.at.17.09.41.mp4