javascript - 在 React 中作为道具传递时区分 img 和 svg
问题描述
假设我有一个组件<MyComponent /> that takes in prop called
src the
src prop can take in
svg as well as a
img`
<MyComponent src={someSvgComponent} />
或者
<MyComponent src={someImgComponent} />
如何检查传入的道具src
是图像还是svg?图像可以是 jpeg|jpg|png|gif 类型
现在src
prop 可以传递文件或文件的路径。
解决方案
imo 最简单的解决方案是添加第二个srcType
道具MyComponent
,它将告诉它作为道具传递的图像类型。这是假设您事先知道传入的是什么图像。
<MyComponent src={someSvgComponent} srcType="svg" />
<MyComponent src={someImgComponent} srcType="img" />
推荐阅读
- tensorflow - 如何减少 TPU 空闲时间?
- python - 如何查看 geopandas 数据框中的所有列?
- azure - 使用 ci cd 部署 Azure Devops
- java -
字符渲染在一段时间后停止工作 - java - wildfly:从类路径目录中读取属性文件
- python-3.x - wget:无法在 google colab 中解析主机地址
- mysql - 在数据库表中维护记录历史
- ant - if 不支持 ant 构建文件中嵌套的 "equals" 元素
- javascript - 如何在javascript中从当前日期开始7天创建一个星期几的数组(即0代表星期日,1代表星期一等)?
- sprite-kit - 为什么为我的 SKSpriteNode 启用 physicBody 会导致异常?