-
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
ServerSideRender: Fix data loading in development mode #62140
Conversation
Size Change: -8 B (0%) Total Size: 1.74 MB
ℹ️ View Unchanged
|
@@ -165,6 +170,9 @@ export default function ServerSideRender( props ) { | |||
fetchRequest === fetchRequestRef.current |
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 guess to be completely correct, we should cancel the fetch request if the component unmounts? Then we also don't need a mount ref?
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 tried only to change what was necessary for the fix + minor colocation.
If you ask me, this whole component needs proper refactoring 😅
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. |
@@ -165,6 +170,9 @@ export default function ServerSideRender( props ) { | |||
fetchRequest === fetchRequestRef.current | |||
) { | |||
setIsLoading( false ); | |||
// Cancel the timeout to show the Spinner. | |||
setShowLoader( false ); |
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.
Why do we need to setShowLoader( false )
on cleanup? Feels odd to set the state on unmounting, since we won't be using that state.
Scratch this, I was looking at the wrong place and thought this was running in an effect.
@@ -98,7 +98,7 @@ export default function ServerSideRender( props ) { | |||
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder, | |||
} = props; | |||
|
|||
const isMountedRef = useRef( true ); | |||
const isMountedRef = useRef( false ); |
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.
But it did mount already, didn't it? Unless I misunderstand something, this looks wrong - can you elaborate?
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.
Setting the initial value doesn't mean that the component was mounted. React can call a component (function) for other reasons without mounting it.
This is a common (anti)pattern when you want to run synchronization only after the initial mount.
const didMount = useRef( false );
useEffect( () => {
didMount.current = true;
return () => { didMount.current = false };
}, [] );
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.
@tyxla, I think this is the third issue I fixed for a similar pattern. I explained why the original implementations were failing in development mode here: #62141 (comment).
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.
Ah, of course, got it. Wow, this component is messy 😉
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, it's bit of noodle soup 🍜
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 tests well for me. 👍
As Ella suggested, there are some opportunities for cleanups and refactors, but they're not related to the PR so they can be addressed separately.
Thanks for testing and reviews, folks! As for refactoring, @kevin940726 proposed hook API for this feature, which should give consumers more control. See #35294 (comment). |
* ServerSideRender: Fix data loading in development mode * Colocate the loading state handlers * Remove leftover testing code Co-authored-by: Mamaduka <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: tyxla <[email protected]>
* ServerSideRender: Fix data loading in development mode * Colocate the loading state handlers * Remove leftover testing code Co-authored-by: Mamaduka <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: tyxla <[email protected]>
What?
Related #61943.
PR fixes the
ServerSideRender
component but is in development mode, where it was stuck in a loading state.Why?
React runs effects extra time in
StrictMode
, and the previousisMounted
logic didn't account for this.How
isMounted
ref value in the existing effect instead of relying on the initial value.Testing Instructions
https://make.wordpress.org/design/
.Patch for enabling modes
Testing Instructions for Keyboard
Same.
Screenshots or screencast
CleanShot.2024-05-30.at.15.55.48.mp4