Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image on Material UI CardMedia

I´m having some troubles on getting an image from props on a CardMedia image:

<Card className={classes.card}>
    <CardMedia 
        className={classes.img}
        image={this.props.recipe.thumbnail}
    />
    <CardContent className={classes.content}>
        <Typography gutterBottom variant="headline" component="h2">
            {this.props.recipe.title}
        </Typography>
        <Typography component="p">
            {this.props.recipe.ingredients}
        </Typography>
    </CardContent>
    <CardActions>
        <Button 
            href={this.props.recipe.href}
            Recipe
        </Button> 
    </CardActions>
</Card>

It just doesnt render at all the images; all the other props values are rendered as they has to. Also as Material UI I had specified the CardMedia height on a JS css.

Does anyone knows why this happens?

like image 255
Marcelo Avatar asked May 10 '18 12:05

Marcelo


3 Answers

I had the Same Issue. Finally Got it Working doing this:

const styles = {
    media: {
      height: 0,
      paddingTop: '56.25%', // 16:9,
      marginTop:'30'
    }
};

     
<CardMedia
  className={classes.media}
  image={require('assets/img/bg2.jpg')} // require image
  title="Contemplative Reptile"
  style={styles.media} // specify styles
/>

You can also check : https://codesandbox.io/s/9zqr09zqjo - No option to load images . The image location is my local

enter image description here

like image 62
1nullpointer Avatar answered Oct 19 '22 20:10

1nullpointer


I have read the docs and also notice that you have to specify the height to display images. While they say you should create a component with style I feel that a simpler way of doing it is by using the style prop directly:

<CardMedia
  style={{height: 0, paddingTop: '56.25%'}}
  image={project.image}
  title="lorem ipsum"
/>

The other options would be to create a style object first and then render the component withStyle as the docs said:

const styles = {
  card: {
    maxWidth: 345,
  },
  media: {
    height: 0,
    paddingTop: '56.25%', // 16:9
  },
};

function SimpleMediaCard(props) {
  const { classes } = props;
  return (
    <div>
      <Card className={classes.card}>
        <CardMedia
          className={classes.media}
          image="/static/images/cards/contemplative-reptile.jpg"
          title="Contemplative Reptile"
        />
      </Card>
    </div>
  );
}

export default withStyles(styles)(SimpleMediaCard);
like image 14
Alexander Luna Avatar answered Oct 19 '22 19:10

Alexander Luna


to set fallback image on CardMedia, you can try this:

import FALLBACK_IMAGE from 'src/assets/images/fallback_image.png';

const onMediaFallback = event => event.target.src = FALLBACK_IMAGE;
<CardMedia
 component="img"
 className={classes.media}
 image="/static/images/cards/contemplative-reptile.jpg"
 title="Contemplative Reptile"
 onError={onMediaFallback}
/>
like image 7
Vaibhav Avatar answered Oct 19 '22 19:10

Vaibhav