-
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
BlockInvalidWarning: Prefer canInsertBlockType
and refactor to hooks.
#49052
Conversation
…sic block not insertable
9e1a3f7
to
56b9e12
Compare
Size Change: -12 B (0%) Total Size: 1.34 MB
ℹ️ View Unchanged
|
const convertToClassic = useCallback( () => { | ||
replaceBlock( block.clientId, blockToClassic( block ) ); | ||
}, [ block, replaceBlock ] ); | ||
|
||
const convertToHTML = useCallback( | ||
() => replaceBlock( block.clientId, blockToHTML( block ) ), | ||
[ block, replaceBlock ] | ||
); |
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.
Suggestion (non-blocking): We can inline these inside useMemo
and use a single method for memoization; also eliminate the blockToClassic
and blockToHTML
helpers.
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.
Yeah, I thought about that, tested it out in 9099bc1
const attemptBlockRecovery = useCallback( () => { | ||
replaceBlock( block.clientId, recoverBlock( block ) ); | ||
}, [ block, replaceBlock ] ); |
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.
Nit: There's no need for the useCallback
for this function; we can inline it. The button component isn't memoized and will re-render alongside the parent.
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.
Yeah, I realise that. I did it for consistency, as otherwise the code looked ugly 😄
|
||
const convertToBlocks = useCallback( | ||
() => replaceBlock( block.clientId, blockToBlocks( block ) ), | ||
const convert = useMemo( |
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 could destructure it, but I decided to keep it this way as it allows me to pass a single convert
value into the following useMemo
dependency array..
Can this take in account the issue #49005? |
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.
Thank you, @talldan!
The refactoring works as expected ✅
Thanks for mentioning that one, I don't think I'll tackle that in this PR, as this is now finalized, but I'll have a think about how that one can be solved. |
What?
Follow up to #49051.
This component should really use
canInsertBlockType
for checking whether a block can be converted to HTML or Classic blocks.At the moment it checks whether the block type is registered, but this doesn't handle situations like
allowedBlocks
for inner blocks.I've also refactored this component to use hooks.
Why?
It should prevent any issues, like a user accidentally inserting an HTML or Classic blocks into a Buttons block.
How?
Use the
canInsertBlockType
selector instead of thegetBlockType
Testing Instructions
Test 1
Test 2
trunk
after the refactorScreenshots or screencast