We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e9ed7c9 commit b3afb9cCopy full SHA for b3afb9c
app/components/ui/Avatar.tsx
@@ -1,4 +1,4 @@
1
-import React from 'react';
+import React, { useState } from 'react';
2
3
interface AvatarProps {
4
src: string;
@@ -7,11 +7,21 @@ interface AvatarProps {
7
}
8
9
export function Avatar({ src, alt, className = '' }: AvatarProps) {
10
+ const [imgSrc, setImgSrc] = useState(src);
11
+ const [error, setError] = useState(false);
12
+
13
+ const handleError = () => {
14
+ setError(true);
15
+ // 设置一个默认的头像 URL
16
+ setImgSrc('/default-avatar.png');
17
+ };
18
19
return (
20
<img
- src={src}
21
+ src={imgSrc}
22
alt={alt}
- className={`w-8 h-8 rounded-full object-cover ${className}`}
23
+ className={`w-8 h-8 rounded-full object-cover ${className} ${error ? 'opacity-50' : ''}`}
24
+ onError={handleError}
25
/>
26
);
27
0 commit comments