Skip to content

⚡ Optimize MediaPickerModal renders using useMemo for derived data#59

Merged
AJFrio merged 1 commit intomainfrom
performance-optimize-media-picker-modal-renders-12557406257922257017
Mar 4, 2026
Merged

⚡ Optimize MediaPickerModal renders using useMemo for derived data#59
AJFrio merged 1 commit intomainfrom
performance-optimize-media-picker-modal-renders-12557406257922257017

Conversation

@AJFrio
Copy link
Copy Markdown
Owner

@AJFrio AJFrio commented Mar 4, 2026

The MediaPickerModal component was previously fetching data for its media library internally when opened, using a useEffect hook to trigger three separate API calls and multiple state updates (setIsLoading, setLibrary). This caused at least three render cycles before any data was shown.

This optimization refactors the component to accept products, collections, and settings as props. It uses useMemo to derive the library array from these props. This approach ensures that the library data is always in sync with the parent state and eliminates the extra render cycles previously required to populate the library.

Furthermore, because the normalizeImageUrl utility can be slow (especially for Google Drive URLs), the normalized URLs are now computed and stored alongside the original URLs within the useMemo block. This prevents redundant normalization during list rendering and scrolling.

Key changes:

  • Component now accepts products, collections, settings, isLoading, and error as props.
  • Removed internal library, isLoading, and error state.
  • Removed imperative loadLibrary fetching logic.
  • Implemented useMemo for derived library data with pre-normalized URLs.
  • Added loading="lazy" to library images to improve initial load performance.

PR created automatically by Jules for task 12557406257922257017 started by @AJFrio

…data

Refactor MediaPickerModal to derive its image library using useMemo from products, collections, and settings props. This eliminates the extra render cycles previously caused by local data fetching and state updates (useEffect + setIsLoading + setLibrary).

The refactor also includes:
- Memoization of normalized image URLs to avoid repeated calls to the potentially slow normalizeImageUrl utility.
- Removal of redundant internal state variables (library, isLoading, error).
- Simplification of the component's useEffect hook.
- Lazy loading for images in the media grid to further enhance performance.

This change follows React performance best practices by treating the media library as derived data rather than a separate state, leading to a more efficient and predictable rendering cycle.

Co-authored-by: AJFrio <20246916+AJFrio@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@AJFrio AJFrio merged commit 7530b90 into main Mar 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant