-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Refactor selectors used in copy-handler #11844
Conversation
|
||
// We only care about this value when the copy is performed | ||
// We call it dynamically in the event handler to avoid unnecessary re-renders. | ||
getBlocks, |
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.
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.
As far as I remember withDispatch
wraps those handlers with proxies to ensure they don’t update on every prop change. So you can’t execute select
outside of those handlers as they will become immediately stale. We will have to document it properly to avoid any bugs. It is advanced usage which is necessary mostly in core, so it might be okey to attack it this way.
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 will open PR and let's discuss it there.
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.
There is one place where I'm not sure whether the logic is exactly the same.
@@ -26,18 +26,18 @@ class CopyHandler extends Component { | |||
} | |||
|
|||
onCopy( event ) { | |||
const { multiSelectedBlocks, selectedBlock } = this.props; | |||
const { selectedBlockClientIds, getBlocks } = this.props; |
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.
const { selectedBlockClientIds, getBlocks } = this.props; | |
const { selectedBlockClientIds = [], getBlocks } = this.props; |
would it be possible to use to simplify next conditional check?
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.
Actually it's an array in all cases, I'll just remove the first check.
@@ -46,12 +46,12 @@ class CopyHandler extends Component { | |||
} | |||
|
|||
onCut( event ) { | |||
const { multiSelectedBlockClientIds } = this.props; | |||
const { selectedBlockClientIds } = this.props; |
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.
The same as above with defaulting to an empty array.
|
||
this.onCopy( event ); | ||
|
||
if ( multiSelectedBlockClientIds.length ) { | ||
this.props.onRemove( multiSelectedBlockClientIds ); | ||
if ( selectedBlockClientIds && selectedBlockClientIds.length ) { |
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.
const selectedBlockClientIds = selectedBlockClientId ? [ selectedBlockClientId ] : getMultiSelectedBlockClientIds();
Should there be a check whethere there is more than one id in here?
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.
You're right 👍
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.
Actually, I'd add a boolean flag hasMultiSelection
it's more secure
b8dde83
to
875d911
Compare
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.
Everything was addressed, thanks. You can 🚢
Extracted form #11811
In this PR, I avoid using the not-so performant selectors
getSelectedBlock
andgetMultiSelectedBlocks
in thewithSelect
HoC and I replace them withgetSelectedBlockClientId
and agetBlocks
selectors only used in the block handler.