Javascript 来自my API的材质UI卡媒体图像数据
在使用Material UI的CardMedia API时,是否有人遇到过此错误 目前,我正在使用材质ui中的Card&CardMedia来呈现来自api的信息,但我在显示图像时遇到了问题,因为我在ComponentMount上拖动图像。在这之前,图像应该已经可用了 这就是错误所在Javascript 来自my API的材质UI卡媒体图像数据,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,在使用Material UI的CardMedia API时,是否有人遇到过此错误 目前,我正在使用材质ui中的Card&CardMedia来呈现来自api的信息,但我在显示图像时遇到了问题,因为我在ComponentMount上拖动图像。在这之前,图像应该已经可用了 这就是错误所在 Warning: Failed prop type: Material-UI: Either `children`, `image`, `src` or `component` prop must be specif
Warning: Failed prop type: Material-UI: Either `children`, `image`, `src` or `component` prop must be specified.
这是母组件的代码
async componentDidMount() {
try {
const getScreams = await axios.get("/screams");
this.setState({ screams: getScreams.data });
} catch (error) {
console.log(error)
}
}
render() {
let recentScreamsMarkup = this.state.screams ? (
this.state.screams.map(item => <Scream key={item.screamId} scream={item}/>)
) : <p>Loading ...</p>
return (
<Grid container spacing={8}>
<Grid item sm={8} xs={12}>
{recentScreamsMarkup}
</Grid>
<Grid item sm={4} xs={12}>
<p>Profile ...</p>
</Grid>
</Grid>
);
}
}
export class Scream extends Component {
render() {
const { classes, scream : {body, commentCount, createdAt, likecount, userHandle, userImage, screamId } } = this.props;
return (
<Card className={classes.card}>
<CardMedia className = {classes.image}
image={userImage}
title={"Profile Image"}
/>
<CardContent className={classes.content}>
<Typography variant="h5" component={Link} to={`/users/${userHandle}`} color= "primary">
{userHandle}
</Typography>
<Typography variant="body2"
color='textSecondary'>
{createdAt}
</Typography>
<Typography variant="body1">
{body}
</Typography>
</CardContent>
</Card>
)
}
}
根据卡介质,它是这样写的:
要显示为背景图像的图像。必须指定image或src prop请注意,调用者必须指定高度,否则图像将不可见
因此,在您的卡片图像css中尝试以下操作:
image: {
height: 140,
}
如果您的图像路径正确(可以根据您自己的要求更改高度),这将解决您的问题。根据卡介质,它是这样写的:
要显示为背景图像的图像。必须指定image或src prop请注意,调用者必须指定高度,否则图像将不可见
因此,在您的卡片图像css中尝试以下操作:
image: {
height: 140,
}
如果您的图像路径正确(可以根据您自己的要求更改高度),这将解决您的问题。对于所有的麻烦,我的愚蠢的提问甚至没有尝试从我的API控制台记录返回的数据,也就是说,我尝试过这样做,但发现返回的数据中没有包含userImage 我使用后端代码进行了更新,现在可以正常工作了
谢谢大家的建议 很抱歉给您添了这么多麻烦,我愚蠢的提问甚至没有尝试对API返回的数据进行控制台日志记录,也就是说,我尝试过这么做,但发现返回的数据中没有包含userImage 我使用后端代码进行了更新,现在可以正常工作了
谢谢大家的建议 构造函数中定义的状态是什么?还可以添加什么是来自api的数据?在postCan上添加了请求的信息,您还可以添加api响应?在构造函数中定义了什么状态?还可以添加什么是来自api的数据?在postCan上添加了请求的信息您也可以添加api响应吗?