-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvatarImage.js
59 lines (52 loc) · 1.23 KB
/
AvatarImage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useState, useEffect } from 'react';
import styled from 'styled-components/native';
import FastImage from 'react-native-fast-image';
const LazyImageView = styled.View`
width: 100%;
position: relative;
overflow: hidden;
height: ${(props) => (props.height ? props.height : '100%')};
`;
const LazyImageElement = styled(FastImage)`
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
align-items: center;
justify-content: center;
z-index: 1;
`;
export default function AvatarImage({ uri, style, ...rest }) {
return (
<LazyImageView style={style} {...rest}>
<Image uri={uri} style={style} />
</LazyImageView>
);
}
const Image = ({ uri, style }) => {
const [imageUri, setImageUri] = useState();
useEffect(() => {
if (!uri) {
return setImageUri('');
}
setImageUri(uri);
}, [uri]);
if (imageUri === undefined) {
return <></>;
}
if (imageUri) {
// Other uri for testing: https://unsplash.it/400/400?image=1
return (
<LazyImageElement
style={style}
source={{
uri: imageUri,
priority: FastImage.priority.normal,
}}
resizeMode={FastImage.resizeMode.contain}
/>
);
}
return <></>;
};