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

Block Bindings: Remove unneeded 'setAttributes' override #58806

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { getBlockType } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
Expand Down Expand Up @@ -36,8 +36,7 @@ const createEditFunctionWithBindingsAttribute = () =>
const { getBlockBindingsSource } = unlock(
useSelect( blockEditorStore )
);
const { getBlockAttributes, updateBlockAttributes } =
useSelect( blockEditorStore );
const { getBlockAttributes } = useSelect( blockEditorStore );

const updatedAttributes = getBlockAttributes( clientId );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we calling a selector during render here? Btw I think we have access to the attributes through props.attributes, so we can shallow clone that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw down below we are mutating the attributes object directly in the store? 🤔

updatedAttributes[ attributeName ] =

Copy link
Member Author

@Mamaduka Mamaduka Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

We can make a few more improvements here, but I'm not familiar enough with the future to confidently make them. So, I decided to stick with changes that won't affect anything.

Suggested improvements:

  • No need to use useBlockEditContext. All required data should be available via props.
  • Clone attributes from props.attributes.
  • Return early if there are no bindings.

Copy link
Contributor

@SantosGuillamot SantosGuillamot Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing the feedback! Actually, I wanted to review that part because in a recent experiment I realized it could be improved as you suggest: link. There, I'm getting the data via props and mutating the attributes in a different way.

Do you think that approach would be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Can you share the PR link? We can continue our discussion there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot that! Here is the PR: link.

It is just an initial experiment that hasn't been tested and needs to be improved. I was considering reusing just the attributesWithBindings part and creating another pull request only with that if we consider it better than the current approach.

if ( updatedAttributes?.metadata?.bindings ) {
Expand Down Expand Up @@ -80,21 +79,12 @@ const createEditFunctionWithBindingsAttribute = () =>
);
}

const registry = useRegistry();

return (
<>
<BlockEdit
key="edit"
attributes={ updatedAttributes }
setAttributes={ ( newAttributes, blockId ) =>
registry.batch( () =>
updateBlockAttributes( blockId, newAttributes )
)
}
{ ...props }
/>
</>
<BlockEdit
key="edit"
attributes={ updatedAttributes }
{ ...props }
/>
);
},
'useBoundAttributes'
Expand Down
Loading