import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import { Link } from 'react-router-dom'; import { UserType } from './Types'; import Icon from './Icon'; import { fetchUserUri } from '../api'; import './Avatar.css'; let isMounted; function Avatar(props) { const [user, setUser] = useState(props.user); useEffect(() => { setUser(props.user); isMounted = true; if (!user.uid && user.uri) { fetchUserUri(user.uri).then(response => { if (isMounted) { setUser(response.data); } }); return () => { isMounted = false; }; } }, [props.user]); return (
{ user.uname ? {user.avatar ? {`${user.uname}`} : } : }
{user.uname} {props.children}
); } export default Avatar; Avatar.propTypes = { user: UserType, link: PropTypes.string, style: PropTypes.object, children: PropTypes.node };