Recently delving into full stack development, I've been experimenting with coding to enhance my understanding of frontend using React JS and Material UI. In the process, I incorporated a card component to display posts from the backend. However, I encountered a challenge when attempting to showcase the profile image in a circular form using CardMedia. To address this issue, I decided to customize the component in the following manner:
import React, { Component } from 'react'
import {Link} from 'react-router-dom';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import DeletePost from './DeletePost'
import PostDialog from './PostDialog'
//MUI
import withStyles from '@material-ui/core/styles/withStyles'
import CardMedia from '@material-ui/core/CardMedia';
import CardContent from '@material-ui/core/CardContent';
import Card from '@material-ui/core/Card';
import Typography from '@material-ui/core/Typography';
import ChatIcon from '@material-ui/icons/Chat'
import MyButton from '../../utils/MyButton';
const styles = {
card: {
display: "flex",
marginBottom:20,
},
image:{
minWidth: 100,
minHeight: 100,
borderRadius: '50%',
objectFit: 'cover'
},
content:{
objectFit: 'cover'
}
}
class Post extends Component {
return (
<Card className={{root: classes.card}}>
<CardMedia
image={userImage}
title="Profile image"
className={classes.image}
/>
<CardContent className={classes.content}>
<Typography
variant="h5"
component={Link}
to={`/users/${userHandle}`}
color="primary">
{userHandle}
</Typography>
{deleteButton}
<Typography variant="body2" color="textSecondary">
{dayjs(createdAt).fromNow()}
</Typography>
<Typography variant="body1">{body}</Typography>
</CardContent>
</Card>
)
}
Post.propTypes = {
user: PropTypes.object.isRequired,
post: PropTypes.object.isRequired,
classes: PropTypes.object.isRequired,
openDialog:PropTypes.bool
}
const mapStateToProps = state =>({
user: state.user
})
export default connect(mapStateToProps)(withStyles(styles)(Post));
After implementation, I noticed that the circular image appeared oval and stretched. Does anyone have a solution or suggestion to rectify this visual discrepancy?