javascript - React 中作为 prop 传递时区分 img 和 svg

标签 javascript reactjs svg

假设我有一个组件 <MyComponent /> that takes in prop called源代码the源代码prop can take in svg as well as a图像`

 <MyComponent src={someSvgComponent} />

或者

 <MyComponent src={someImgComponent} />

如何检查src中是否传递了 Prop 是图像还是svg?图像的类型可以是 jpeg|jpg|png|gif

现在是src prop 可以传递文件,也可以传递文件的路径。

最佳答案

在我看来,最简单的解决方案是向 MyComponent 添加第二个 srcType 属性,该属性将告诉它作为属性传递的图像类型。假设您事先知道传入的图像是什么。

<MyComponent src={someSvgComponent} srcType="svg" />

<MyComponent src={someImgComponent} srcType="img" />

关于javascript - React 中作为 prop 传递时区分 img 和 svg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483737/

相关文章:

javascript - 来自 csv 的 D3 中的 svg 行(作为单独的行而不是一行)-不显示

javascript - AJAX:$_GET 不返回任何值

javascript - 鼠标单击对象数组javascript Canvas

reactjs - setState 未在 componentWillReceiveProps 内部更新

javascript - React Native 中 componentDidMount() 警告中的异步 SetState

javascript - d3 如何限制拖放区域

javascript - 用于大型组织结构图的 Canvas 或 SVG(或混合)

javascript - 带有 Bootstrap 4 Grid 的 Angular Material 2

javascript - 如何创建多个表并在每个表中存储json数据

javascript - 我如何在 React Redux 中访问存储状态?