-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ServerSideRender: Colocate delayed spinner logic #70147
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
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 |
---|---|---|
|
@@ -69,7 +69,22 @@ function DefaultErrorResponsePlaceholder( { response, className } ) { | |
return <Placeholder className={ className }>{ errorMessage }</Placeholder>; | ||
} | ||
|
||
function DefaultLoadingResponsePlaceholder( { children, showLoader } ) { | ||
function DefaultLoadingResponsePlaceholder( { children, isLoading } ) { | ||
const [ showLoader, setShowLoader ] = useState( false ); | ||
|
||
useEffect( () => { | ||
if ( ! isLoading ) { | ||
setShowLoader( false ); | ||
return; | ||
} | ||
|
||
// Schedule showing the Spinner after 1 second. | ||
const timeout = setTimeout( () => { | ||
setShowLoader( true ); | ||
}, 1000 ); | ||
return () => clearTimeout( timeout ); | ||
}, [ isLoading ] ); | ||
|
||
return ( | ||
<div style={ { position: 'relative' } }> | ||
{ showLoader && ( | ||
|
@@ -101,7 +116,6 @@ export default function ServerSideRender( props ) { | |
} = props; | ||
|
||
const isMountedRef = useRef( false ); | ||
const [ showLoader, setShowLoader ] = useState( false ); | ||
const fetchRequestRef = useRef(); | ||
const [ response, setResponse ] = useState( null ); | ||
const prevProps = usePrevious( props ); | ||
|
@@ -127,11 +141,6 @@ export default function ServerSideRender( props ) { | |
|
||
setIsLoading( true ); | ||
|
||
// Schedule showing the Spinner after 1 second. | ||
const timeout = setTimeout( () => { | ||
setShowLoader( true ); | ||
}, 1000 ); | ||
|
||
let sanitizedAttributes = | ||
attributes && | ||
__experimentalSanitizeBlockAttributes( block, attributes ); | ||
|
@@ -185,9 +194,6 @@ export default function ServerSideRender( props ) { | |
fetchRequest === fetchRequestRef.current | ||
) { | ||
setIsLoading( false ); | ||
// Cancel the timeout to show the Spinner. | ||
setShowLoader( false ); | ||
clearTimeout( timeout ); | ||
} | ||
} ) ); | ||
|
||
|
@@ -221,7 +227,7 @@ export default function ServerSideRender( props ) { | |
|
||
if ( isLoading ) { | ||
return ( | ||
<LoadingResponsePlaceholder { ...props } showLoader={ showLoader }> | ||
<LoadingResponsePlaceholder { ...props } isLoading={ isLoading }> | ||
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. Minor, but if we're worried about back-compat, I can revert the prop name, or just add a changelog entry about it. 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 think this component is rendered when 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. That's true, mostly passing the value as a safeguard for future changes. Same as here - #70147 (comment). |
||
{ hasResponse && ! hasError && ( | ||
<RawHTML className={ className }>{ response }</RawHTML> | ||
) } | ||
|
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.
Note: This is just a safeguard for the future. Currently, the component will unmount when
isLoading
settles onfalse
.