百度,google搜了一下,发现不少教怎么用的,就是没有封装组件的.react
实际这个东西,用起来仍是很简单的.bash
今天正好封装了个组件.svg
今天算是写react的第三个月了.react仍是比较好上手的.ui
几种应用:this
这种是JQuery的.明显不是想要的,略过google
这种用classname找的方法.感受很差.spa
最后这个才像是须要的.code
/**
* 简单生成条形码
* {
* width: 2,//较细处条形码的宽度
* height: 100, //条形码的宽度,无高度直接设置项,由位数决定,能够经过CSS去调整,见下
* quite: 10,
* format: "CODE128",
* displayValue: false,//是否在条形码下方显示文字
* font:"monospace",
* textAlign:"center",
* fontSize: 12,
* backgroundColor:"",
* lineColor:"#000"//条形码颜色
* }
*/
class SimpleBarcode extends Component {
componentDidMount() {
this.createBarcode();
}
componentWillReceiveProps(nextProps) {
if (this.props !== nextProps) {
this.createBarcode();
}
}
createBarcode = () => {
if (!this.barcode) return;
const {
width = 1, height = 35, margin = 0, label, displayValue = true,
} = this.props;
if (!label) {
return;
}
Barcode(this.barcode, label, {
displayValue, // 是否由Barcode显示二维码下面的值
width, // 每条线的宽度
height, // 高度
margin, //边距
});
};
render() {
const {
labelClassName, label, labelStyle, className, style, displayValue = true,
} = this.props;
return (
<div className={className} style={style}>
<svg
ref={(ref) => {
this.barcode = ref;
}}
/>
{displayValue ? null : <p className={labelClassName} style={labelStyle}>{label}</p>} // 自定义样式的值显示
</div>
);
}
}
复制代码
<SimpleBarcode
label={123123}
height="45"
width="1"//这里只能是大于1的整数
/>
复制代码
期待你的评论,点赞component