import React 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'; class Avatar extends React.Component { constructor(props) { super(props); this.state = { user: props.user }; } componentDidMount() { let user = this.state.user; if (!user.uid && user.uri) { fetchUserUri(user.uri).then(response => { this.setState({ user: response.data }); }); } } shouldComponentUpdate(nextProps, nextState) { return this.state.user.avatar != nextState.user.avatar || this.state.user.uid != nextState.user.uid || this.props.user.avatar != nextProps.user.avatar || this.props.user.uid != nextProps.user.uid; } componentDidUpdate(prevProps, prevState, shouldReload) { this.setState({ user: this.props.user }); } render() { let user = this.state.user; return (
{ user.uname ? {user.avatar ? {`${user.uname}`} : } : }
{user.uname} {this.props.children}
); } } export default Avatar; export const AvatarLink = React.memo(props => { return ( {`${props.user.uname}`} {props.user.uname} ); }); Avatar.propTypes = { user: UserType, link: PropTypes.string, style: PropTypes.object, children: PropTypes.node };