ui
ui copied to clipboard
Avatar component makes 2 calls to the image src
The Avatar component makes 2 calls in every render to the image src

Sorry, isn't this related to React Strictmode?
The screenshot depicts the live version of the ui.shadcn.com also, trying pure radix avatar doesn't seem to have this effect of repeated calls
Oh I see. Thought you were using it in your own app @0xdhrv
When trying the pure radix avatar in next.js i still get the double fetch. But if you change the radix <AvatarPrimitive.Image />
to the <Image />
component from next.js then the issue resolves... This also seems to have a dramatic effect on performance due to the image optimization.
But anyway this is probably an issue with radix..
FYI. i have also tried to run the <AvatarPrimitive.Image asChild />
with the image component. I get the same issue..
Here is my Avatar component, not perfect.
"use client";
import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";
import { cn } from "@/lib/utils";
import Image from "next/image";
const Avatar = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Root
ref={ref}
className={cn(
"relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",
className
)}
{...props}
/>
));
Avatar.displayName = AvatarPrimitive.Root.displayName;
const AvatarImage = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Image>,
React.ComponentPropsWithoutRef<typeof Image>
>(({ className, src, alt, ...props }, ref) => (
<Image
src={src}
alt={alt}
{...props}
className={cn("aspect-square h-full w-full", className)}
/>
));
AvatarImage.displayName = AvatarPrimitive.Image.displayName;
const AvatarFallback = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Fallback>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Fallback
ref={ref}
className={cn(
"flex h-full w-full items-center justify-center rounded-full bg-neutral-100 dark:bg-neutral-800",
className
)}
{...props}
/>
));
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName;
export { Avatar, AvatarImage, AvatarFallback };
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.