-
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
ESLint: Fix a couple of React Compiler reassignment errors #66331
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ import { | |||||||||||||||||||||||
ToolbarGroup, | ||||||||||||||||||||||||
} from '@wordpress/components'; | ||||||||||||||||||||||||
import { useDispatch, useSelect } from '@wordpress/data'; | ||||||||||||||||||||||||
import { renderToString } from '@wordpress/element'; | ||||||||||||||||||||||||
import { renderToString, useRef } from '@wordpress/element'; | ||||||||||||||||||||||||
import { __ } from '@wordpress/i18n'; | ||||||||||||||||||||||||
import { useInstanceId } from '@wordpress/compose'; | ||||||||||||||||||||||||
import { store as noticeStore } from '@wordpress/notices'; | ||||||||||||||||||||||||
|
@@ -59,14 +59,14 @@ export default function TableOfContentsEdit( { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
// If a user clicks to a link prevent redirection and show a warning. | ||||||||||||||||||||||||
const { createWarningNotice, removeNotice } = useDispatch( noticeStore ); | ||||||||||||||||||||||||
let noticeId; | ||||||||||||||||||||||||
const noticeIdRef = useRef(); | ||||||||||||||||||||||||
const showRedirectionPreventedNotice = ( event ) => { | ||||||||||||||||||||||||
event.preventDefault(); | ||||||||||||||||||||||||
// Remove previous warning if any, to show one at a time per block. | ||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Do we need reassignment here? The P.S. The Latest Posts block has similar logic. gutenberg/packages/block-library/src/latest-posts/edit.js Lines 159 to 169 in acf661c
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dispatching a notice won't rerender the 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Even better follow-up here: #66409 |
||||||||||||||||||||||||
createWarningNotice( __( 'Links are disabled in the editor.' ), { | ||||||||||||||||||||||||
id: noticeId, | ||||||||||||||||||||||||
id: noticeIdRef.current, | ||||||||||||||||||||||||
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.
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 amap
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.