components/PostCard.js

    import React, {useMemo} from 'react';
    import {View, StyleSheet, Text, Image, Pressable} from 'react-native';
    
    function PostCard({user, photoURL, description, createdAt, id}) {
      const date = useMemo(
        () => (createdAt ? new Date(createdAt._seconds * 1000) : new Date()),
        [createdAt],
      );
    
      const onOpenProfile = () => {
        // TODO: 사용자 프로필 화면 열기
      };
    
      return (
        <View style={styles.block}>
          <View style={[styles.head, styles.paddingBlock]}>
            <Pressable style={styles.profile} onPress={onOpenProfile}>
              source={
                user.photoURL
                  ? {
                    uri: user.photoURL,
                  }
                : require('../assets/user.png')
              }
              resizeMode="cover"
              style={styles.avatar}
            />
            <Text style={styles.displayName}>{user.displayName}</Text>
          </Pressable>
          </View>
          <Image
            source={{uri: photoURL}}
            style={styles.image}
            resizeMethod="resize"
            resizeMode="cover"
          />
          <View style={styles.paddingBlock}>
            <Text style={styles.description}>{description}</Text>
            <Text date={date} style={styles.date}>
              {date.toLocaleString()}
            </Text>
          </View>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      block: {
        paddingTop: 16,
        paddingBottom: 16,
      },
      avatar: {
        width: 32,
        height: 32,
        borderRadius: 16,
      },
      paddingBlock: {
        paddingHorizontal: 16,
      },
      head: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        marginBottom: 16,
      },
      profile: {
        flexDirection: 'row',
        alignItems: 'center',
      },
      displayName: {
        lineHeight: 16,
        fontSize: 16,
        marginLeft: 8,
        fontWeight: 'bold',
      },
      image: {
        backgroundColor: '#bdbdbd',
        width: '100%',
        aspectRatio: 1,
        marginBottom: 16,
      },
      description: {
        fontSize: 16,
        lineHeight: 24,
        marginBottom: 8,
      },
      date: {
        color: '#757575',
        fontSize: 12,
        lineHeight: 18,
      },
    });
    
    export default PostCard;

    여기서 styles.headjustifyContent: 'space-between' 스타일을 부여했습니다. 이 스타일을 적용한 이유는 해당 포스트가 자신의 포스트라면 우측 상단에 점 3개 아이콘을 보여주기 위함입니다. 이 아이콘을 누르면 사용자는 포스트를 삭제하거나 수정할 수 있습니다.

    그리고 사용자 프로필 이미지를 보여줄 때 만약 프로필 이미지가 존재하지 않으면 assets 디렉터리에 저장한 기본 프로필 이미지를 보여주도록 설정했습니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.