-
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
ESLint: Fix a couple of React Compiler reassignment errors #66331
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. |
@@ -133,15 +135,15 @@ function ListViewBranch( props ) { | |||
const blockCount = filteredBlocks.length; | |||
// The appender means an extra row in List View, so add 1 to the row count. | |||
const rowCount = showAppender ? blockCount + 1 : blockCount; | |||
let nextPosition = listPosition; | |||
nextPositionRef.current = listPosition; |
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.
This probably goes against the "don't read/write refs during render" rule, no?
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 thought it might, but it doesn't really trigger an error. And I think we can find similar usage across the repo already, so 🤷
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 wonder why exactly the compiler didn't like the nextPosition
local variable. Maybe it's that it's incremented in a map
callback and the compiler thinks that the callback is an event handler?
nextPosition
is a legit local variable, assigned and used only during the render, and the value doesn't need to survive the render function. The ref is not really needed. Reading/writing it is safe because, well, it's used as a local variable.
Size Change: +12 B (0%) Total Size: 1.81 MB
ℹ️ View Unchanged
|
removeNotice( noticeId ); | ||
noticeId = `block-library/core/table-of-contents/redirection-prevented/${ instanceId }`; | ||
removeNotice( noticeIdRef.current ); | ||
noticeIdRef.current = `block-library/core/table-of-contents/redirection-prevented/${ instanceId }`; |
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.
Do we need reassignment here? The instanceId
shouldn't change between rerenders; we can always use clientId
if it does.
P.S. The Latest Posts block has similar logic.
gutenberg/packages/block-library/src/latest-posts/edit.js
Lines 159 to 169 in acf661c
let noticeId; | |
const showRedirectionPreventedNotice = ( event ) => { | |
event.preventDefault(); | |
// Remove previous warning if any, to show one at a time per block. | |
removeNotice( noticeId ); | |
noticeId = `block-library/core/latest-posts/redirection-prevented/${ instanceId }`; | |
createWarningNotice( __( 'Links are disabled in the editor.' ), { | |
id: noticeId, | |
type: 'snackbar', | |
} ); | |
}; |
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.
Does the version in Latest Posts really work? It seems to me that the noticeId
is reset to undefined
on each rerender of the block's Edit
component. When you click and the showRedirectionPreventedNotice
handler is called, the created notice id is saved, but on the next rerender it's lost again.
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.
Dispatching a notice won't rerender the Edit
component, so the noticeId
is retained. But yes, the value might get lost.
By the way, the following snippet has the same effect and displays only a single notice.
const showRedirectionPreventedNotice = ( event ) => {
event.preventDefault();
createWarningNotice( __( 'Links are disabled in the editor.' ), {
id: `block-library/core/latest-posts/redirection-prevented/${ instanceId }`,
type: 'snackbar',
} );
};
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.
Dispatching a notice won't rerender, but if the component is rerendered for any other reason then the noticeId
value will be lost. Adding the ref is a correct fix, and it should be applied also to the LatestPostsEdit
component.
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 agree with @jsnajdr that the ref is the correct fix.
I'm happy to follow up with fixing that in LatestPostsEdit
if we all agree on it.
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.
Followed up here for addressing in the Latest Posts block: #66404
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.
Even better follow-up here: #66409
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.
🚢
…#66331) Co-authored-by: tyxla <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: jsnajdr <[email protected]>
What?
Resolves a few straightforward violations of the React Compiler reassignment rule:
Why?
To resolve a few errors related to reassignment in #61788.
How?
We're using refs instead, to preserve the pre-existing non-reactive behavior.
Testing Instructions
Testing Instructions for Keyboard
Same
Screenshots or screencast
None