質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Material-UI

Material-UIは、Material Designを利用可能なオープンソースのReact向けUIコンポーネントキットです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

3939閲覧

React Material-UIでのCardsで画像が表示されない

退会済みユーザー

退会済みユーザー

総合スコア0

Material-UI

Material-UIは、Material Designを利用可能なオープンソースのReact向けUIコンポーネントキットです。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2019/04/29 11:02

前提・実現したいこと

React Material-UIでのCardsで画像が表示されないです。
公式のドキュメントのデモを使っています。

image="/static/images/cards/paella.jpg" の画像を表示することができませんでした。
原因が不明なので、ご教示いただきたいです。

該当のソースコード

import React from 'react'; import PropTypes from 'prop-types'; import { withStyles } from '@material-ui/core/styles'; import classnames from 'classnames'; import Card from '@material-ui/core/Card'; import CardHeader from '@material-ui/core/CardHeader'; import CardMedia from '@material-ui/core/CardMedia'; import CardContent from '@material-ui/core/CardContent'; import CardActions from '@material-ui/core/CardActions'; import Collapse from '@material-ui/core/Collapse'; import Avatar from '@material-ui/core/Avatar'; import IconButton from '@material-ui/core/IconButton'; import Typography from '@material-ui/core/Typography'; import red from '@material-ui/core/colors/red'; import FavoriteIcon from '@material-ui/icons/Favorite'; import ShareIcon from '@material-ui/icons/Share'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import MoreVertIcon from '@material-ui/icons/MoreVert'; const styles = theme => ({ card: { maxWidth: 400, }, media: { height: 0, paddingTop: '56.25%', // 16:9 }, actions: { display: 'flex', }, expand: { transform: 'rotate(0deg)', marginLeft: 'auto', transition: theme.transitions.create('transform', { duration: theme.transitions.duration.shortest, }), }, expandOpen: { transform: 'rotate(180deg)', }, avatar: { backgroundColor: red[500], }, }); class RecipeReviewCard extends React.Component { state = { expanded: false }; handleExpandClick = () => { this.setState(state => ({ expanded: !state.expanded })); }; render() { const { classes } = this.props; return ( <Card className={classes.card}> <CardHeader avatar={ <Avatar aria-label="Recipe" className={classes.avatar}> R </Avatar> } action={ <IconButton> <MoreVertIcon /> </IconButton> } title="Shrimp and Chorizo Paella" subheader="September 14, 2016" /> <CardMedia className={classes.media} image="/static/images/cards/paella.jpg" title="Paella dish" /> <CardContent> <Typography component="p"> This impressive paella is a perfect party dish and a fun meal to cook together with your guests. Add 1 cup of frozen peas along with the mussels, if you like. </Typography> </CardContent> <CardActions className={classes.actions} disableActionSpacing> <IconButton aria-label="Add to favorites"> <FavoriteIcon /> </IconButton> <IconButton aria-label="Share"> <ShareIcon /> </IconButton> <IconButton className={classnames(classes.expand, { [classes.expandOpen]: this.state.expanded, })} onClick={this.handleExpandClick} aria-expanded={this.state.expanded} aria-label="Show more" > <ExpandMoreIcon /> </IconButton> </CardActions> <Collapse in={this.state.expanded} timeout="auto" unmountOnExit> <CardContent> <Typography paragraph>Method:</Typography> <Typography paragraph> Heat 1/2 cup of the broth in a pot until simmering, add saffron and set aside for 10 minutes. </Typography> <Typography paragraph> Heat oil in a (14- to 16-inch) paella pan or a large, deep skillet over medium-high heat. Add chicken, shrimp and chorizo, and cook, stirring occasionally until lightly browned, 6 to 8 minutes. Transfer shrimp to a large plate and set aside, leaving chicken and chorizo in the pan. Add pimentón, bay leaves, garlic, tomatoes, onion, salt and pepper, and cook, stirring often until thickened and fragrant, about 10 minutes. Add saffron broth and remaining 4 1/2 cups chicken broth; bring to a boil. </Typography> <Typography paragraph> Add rice and stir very gently to distribute. Top with artichokes and peppers, and cook without stirring, until most of the liquid is absorbed, 15 to 18 minutes. Reduce heat to medium-low, add reserved shrimp and mussels, tucking them down into the rice, and cook again without stirring, until mussels have opened and rice is just tender, 5 to 7 minutes more. (Discard any mussels that don’t open.) </Typography> <Typography> Set aside off of the heat to let rest for 10 minutes, and then serve. </Typography> </CardContent> </Collapse> </Card> ); } } RecipeReviewCard.propTypes = { classes: PropTypes.object.isRequired, }; export default withStyles(styles)(RecipeReviewCard);

試したこと

公式の階層通りにフォルダを作成し、画像を用意しました。
ローカルだけではなく、デプロイしても表示ができませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こんにちは。

ご質問に挙げているのは、Cardsのこのデモだと思いますが、以下の手順でソースをコピーしたアプリをローカルに作成しました。

  • create-react-app でアプリのひな形を作成(アプリ名は、 q186994)
  • 余分なファイルやコードを削除し、デフォルトで作成された App を、デモに含まれる demo.jsで置き換え
  • public/ の下に、static/images/cards/ を作り、ここに画像 paella.jpg を入れる。

上記を行って yarn start すると、以下のように画像が表示されます。

イメージ説明

以下にソースを上げておいたので参考にしてください。

上記のレポジトリでは、画像はここです。

投稿2019/04/29 12:42

編集2019/04/29 13:55
jun68ykt

総合スコア9058

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/04/29 13:32

ありがとうございます、表示できました create-react-appでは画像を格納するフォルダが決められているのでしょうか?
jun68ykt

2019/04/29 13:45

> 表示できました とのことでよかったです。 > create-react-appでは画像を格納するフォルダが決められているのでしょうか? ソースに image="/static/images/cards/paella.jpg" と書かれていて、このパスに該当するフォルダは、 create-react-appで作られたアプリだと、 public/static/images/cards/ に相当する、ということですね。ですので、ここを変更して例えば、 image="/imgs/paella.jpg" としたいのであれば、 public/ の下に imgs/ を作ってそこにpaella.jpgをコピーということになるかと思います。 create-react-appで作られるアプリで決められている(というか設定されている)ことは、パスの一番上の "/" に該当するフォルダが、 public/ であるということです。なので、index.html も public/ の下に生成されます。
退会済みユーザー

退会済みユーザー

2019/04/30 04:20

ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問