我已经在React中编程了很短的时间,并且对如何进行操作有一些疑问。我有一个组件,将导入的svg显示为图像。
import Arrow from '../arrow.svg';
if (!isChecked) {
config.email = {
clickable: true,
image: Arrow,
onClick: () =>
inProfile
? dispatch(notification(CHANGE_STATUS))
: browserHistory.push(/checkOut),
};
}
现在,我想用从外部库中导入的图像替换项目中的图像,该图像如下所示
<Img type="right-arrow" />
这是一个现在正在我的代码中工作的示例
renderImage() {
return <Img type="right-arrow" />;
}
render() {
return(
{this.renderHeader}
{this.renderTitle}
{this.renderBody}
{this.renderImage}
);
}
如何使用此新导入的组件代替以前使用的图像?
我尝试了几种方法,但是无法渲染。
我尝试过的最后一件事是以下
import Image from '@market/image-market';
if (!isChecked) {
config.email = {
clickable: true,
image: <Img type="right-arrow" />
onClick: () =>
inProfile
? dispatch(notification(CHANGE_STATUS))
: browserHistory.push(/checkOut),
};
}
我看不到如何在此结构中使用它。如果有人看到我的错误。非常感谢您的时间和提前的帮助
我认为您对引擎盖下发生的事情感到有些困惑。
config.emailAddress.image
像这样使用<>
{config.emailAddress.image}
</>
您应该传递image: <Img type="right-arrow" />
而不是声明一个函数。
image: () => <Img type="right-arrow" />
,您将必须调用函数以渲染组件。<>
{config.emailAddress.image()}
</>
我认为第一种方法总是比较容易。
这也应该有助于您理解为什么可以如此轻松地加载SVG。
SVG可以导入并直接用作React代码中的React组件。该图像不会作为单独的文件加载,而是沿HTML呈现。
有关实现的详细信息,请参见此stackoverflow答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句