-
Notifications
You must be signed in to change notification settings - Fork 342
/
qr-canvas.tsx
38 lines (33 loc) · 1 KB
/
qr-canvas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { HTMLProps } from 'react'
import QRCode from 'qrcode'
export interface Props extends HTMLProps<HTMLCanvasElement> {
toEncode: string
errorHandler: (e: Error) => void
}
export default class QRCanvas extends React.PureComponent<Props> {
static defaultProps: Partial<Props> = {
errorHandler: (e) => undefined,
}
private canvasRef = React.createRef<HTMLCanvasElement>()
async componentDidMount() {
try {
await QRCode.toCanvas(
this.canvasRef.current!,
this.props.toEncode,
{
color: {
dark: '#FAFAFA',
light: '#303139',
},
width: 400,
},
)
} catch (err) {
this.props.errorHandler(err)
}
}
render() {
const { toEncode, errorHandler, ...canvasProps } = this.props
return <canvas ref={this.canvasRef} {...canvasProps} />
}
}