假设我有一个组件 <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/