Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-day committed Mar 15, 2023
1 parent 9fc3d65 commit 0884c93
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
7 changes: 2 additions & 5 deletions wp-modules/app/js/src/components/Patterns/PatternGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import loadable from '@loadable/component';
import { patternManager } from '../../globals';

// Hooks
import useForceRerend from '../../hooks/useForceRerend';
import useForceRerender from '../../hooks/useForceRerender';

// Components
const PatternPreview: PatternPreviewType = loadable(
Expand All @@ -29,10 +29,7 @@ type Props = {

/** Render the patterns in a grid, or a message if no patterns are found. */
export default function PatternGrid( { themePatterns }: Props ) {
// If the window is resized, trigger a fresh render of the grid.
// Passing `themePatterns` also re-renders while searching or changing categories.
// Helps ensure PatternPreview iFrames are the right size.
useForceRerend( [ themePatterns ] );
useForceRerender( [ themePatterns ] );

return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLayoutEffect, useState, useRef } from '@wordpress/element';
import { useLayoutEffect, useState } from '@wordpress/element';

type WindowDimensions = [ number, number ];

Expand All @@ -7,25 +7,19 @@ type WindowDimensions = [ number, number ];
*
* Optionally include an array of dependencies to trigger a re-render.
*/
export default function useForceRerend< T extends unknown >(
dependencies: T[] = []
export default function useForceRerender< T extends unknown >(
dependencies: T[]
) {
const [ , setForceUpdate ] = useState< T[] | WindowDimensions >();
const initialRender = useRef( true );

useLayoutEffect( () => {
// Do not cause a re-render on first render.
if ( initialRender.current ) {
initialRender.current = false;
} else {
setForceUpdate( dependencies );
}
setForceUpdate( dependencies );

function updateSizeAndRerender() {
setForceUpdate( [
window.innerWidth,
window.innerHeight,
] as WindowDimensions );
] );
}

window.addEventListener( 'resize', updateSizeAndRerender );
Expand Down

0 comments on commit 0884c93

Please sign in to comment.