Css 使用React-Typescript显示图像的按钮
我正在使用React-TypescriptCss 使用React-Typescript显示图像的按钮,css,reactjs,typescript,emotion,Css,Reactjs,Typescript,Emotion,我正在使用React-Typescript 如何将图像添加到按钮? 我试着这样做,没有错误,但我得到了一个空白按钮 如何在这里使用情感CSS设计我的按钮元素 从“React”导入React; 功能按钮(道具){ 让样式={ 宽度:24, 身高:24, src:“../images/abc.png” }; 返回( props.handleClick()}>{props.label} ); } 返回( ); } src将是背景:url(../images/abc.png) 也许您混淆了srcprop
我试着这样做,没有错误,但我得到了一个空白按钮李>
从“React”导入React;
功能按钮(道具){
让样式={
宽度:24,
身高:24,
src:“../images/abc.png”
};
返回(
props.handleClick()}>{props.label}
);
}
返回(
);
}
src
将是背景:url(../images/abc.png)
也许您混淆了src
prop,而img
HTML元素会有这个属性
也许您应该在按钮中包含嵌套的img
组件
工作示例:Damian的答案在技术上是正确的,但是如果您使用的是webpack,则需要像这样导入图像:
import abc from "../images/abc.png";
function PostButton(props){
let style = {
width:24,
height:24,
background: `url(${abc})`,
};
return(
<button style={style} onClick={() => props.handleClick()}>{props.label}</button>
);
}
然后像这样使用它:
import abc from "../images/abc.png";
function PostButton(props){
let style = {
width:24,
height:24,
background: `url(${abc})`,
};
return(
<button style={style} onClick={() => props.handleClick()}>{props.label}</button>
);
}
功能按钮(道具){
让样式={
宽度:24,
身高:24,
背景:`url(${abc})`,
};
返回(
props.handleClick()}>{props.label}
);
}