-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Fix passing bindings context to canUserEditValue
#65599
Block Bindings: Fix passing bindings context to canUserEditValue
#65599
Conversation
blockBindings, | ||
name, | ||
clientId, | ||
updatedContext, |
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.
I think this should be passing context
, and the selector should return the updated context together with attributes:
const { boundAttributes, updatedContext } = useSelect( () => {
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 seems moving updatedContext
inside the useSelect
triggers the same issue reported here where it throws the warning:
The 'useSelect' hook returns different values when called with the same state and parameters. This can lead to unnecessary rerenders.
I can take a look at the solutions suggested there, but I was wondering first: Would it be safe to directly modify the context prop? Instead of doing updatedContext[ key ] = blockContext[ key ];
do directly context[ key ] = blockContext[ key ];
.
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 probably bad idea to mutate anything that React controls - props, context, etc.
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.
I've started exploring moving the updatedContext to a useMemo
call in this pull request.
@@ -139,6 +140,11 @@ export const withBlockBindingSupport = createHigherOrderComponent( | |||
continue; | |||
} | |||
|
|||
// Populate context. | |||
for ( const key of source.usesContext || [] ) { |
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.
With https://github.com/WordPress/gutenberg/pull/65599/files#r1772886078 applied, this can be refactored to:
let updatedContext = context;
if ( source.usesContext?.length ) {
let updatedContext = { ...context };
for ( const key of source.usesContext ) {
updatedContext[ key ] = blockContext[ key ];
}
}
Size Change: -28 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
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. |
…65599) * Pass updated context as `context` prop * Use `updatedContext` in pattern overrides Co-authored-by: SantosGuillamot <[email protected]> Co-authored-by: gziolo <[email protected]>
I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: 44e4896 |
One remark is that from this point, the context passed to the source API methods is what the block defines with |
Thanks for fixing! |
I just cherry-picked this PR to the release/19.3 branch to get it included in the next release: 9b8b551 |
…65599) * Pass updated context as `context` prop * Use `updatedContext` in pattern overrides Co-authored-by: SantosGuillamot <[email protected]> Co-authored-by: gziolo <[email protected]>
What?
This PR ensure the
usesContext
property from registered sources is taken into account in all the scenarios, includingcanUserEditValue
which isn't working as expected right now.Why?
After this core commit, the context is populated dynamically both in the server and in the editor depending on the block bindings existing. However, the updated context is only pass to
getValues
andsetValues
.This is causing e2e tests to fail.
How?
I'm passing the updated context as
props.context
to ensure it is always available. This context is only updated once the block is connected to the relevant sources.Testing Instructions
Failing e2e tests should pass. Manual testing: