import React, { useState, useEffect } from 'react';
import { ScrollView } from 'react-native';
import API from './../API.js';
import Post from './Post.js';

let SinglePostComponent = ({ postId }) => {
    let [post, setPost] = useState({});
    useEffect(() => {
        let subscribed = true;
        let getData = async ()=>{
            if (postId){
                let post = await API.getPost(postId);
                if(subscribed) setPost(post);
            }
        }
        getData();
        return () => {
            subscribed = false;
        }
    }, [postId]);
    return (post._id ? (
        <ScrollView>
        <Post post={post}/>
        </ScrollView>
    ) : null);
};

export default React.memo(SinglePostComponent);
