Skip to content

Commit a7ad349

Browse files
committed
refactore to use hook
1 parent 31eeffb commit a7ad349

File tree

6 files changed

+200
-329
lines changed

6 files changed

+200
-329
lines changed

.eslintrc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"no-floating-decimal": 2,
1717
"no-inner-declarations": [2, "both"],
1818
"no-lonely-if": 2,
19-
"no-multiple-empty-lines": [2, {"max": 3}],
19+
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 1 }],
2020
"no-self-compare": 2,
2121
"no-underscore-dangle": 0,
2222
"no-use-before-define": 0,
@@ -40,7 +40,14 @@
4040
"strict": [0],
4141
"valid-jsdoc": 2,
4242
"wrap-iife": [2, "any"],
43-
"yoda": [1, "never"]
43+
"yoda": [1, "never"],
44+
"no-multi-spaces": "error",
45+
"no-trailing-spaces": "error",
46+
"comma-spacing": ["error", { "before": false, "after": true }],
47+
"no-spaced-func": "error",
48+
"no-whitespace-before-property": "error",
49+
"space-before-function-paren": ["error", "never"],
50+
"space-infix-ops": "error",
4451
},
4552
"plugins": [
4653
"react"

.eslintrc.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ module.exports = {
2929
'stories/**'
3030
]
3131
}
32-
]
33-
},
32+
],
33+
"no-trailing-spaces": "error",
34+
"comma-spacing": ["error", { "before": false, "after": true }],
35+
"no-spaced-func": "error",
36+
"no-whitespace-before-property": "error",
37+
"space-before-function-paren": ["error", "never"],
38+
"space-infix-ops": "error",
39+
}
3440
};

src/DisplayQRCode.js

Lines changed: 50 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,57 @@
1-
import React, {Component} from 'react';
1+
import React, {Component,useEffect,useState} from 'react';
22
import QRCode from "qrcode.react";
33

4-
5-
6-
export default class DisplayQRCode extends Component {
7-
constructor(props){
8-
super(props);
9-
this.onWindowResize=this.onWindowResize.bind(this);
4+
const styles={
5+
container:{
6+
display:"flex",
7+
flexDirection:"column",
8+
justifyContent:"flex-start",
9+
alignItems:"center",
10+
},
11+
qrCodeContainer:{
12+
padding:5,
13+
display:"flex",
14+
width:"100%",
15+
flexDirection:"column",
16+
justifyContent:"flex-start",
17+
alignItems:"center"
18+
},
19+
label:{
20+
marginTop:10,
21+
fontSize: 22,
22+
color: "#4880ED",
23+
fontWeight: 300
1024
}
11-
componentDidMount() {
12-
window.addEventListener("resize", this.onWindowResize);
13-
}
14-
onWindowResize(){
15-
16-
this.forceUpdate();
17-
}
18-
componentWillUnmount() {
19-
window.removeEventListener("resize", this.onWindowResize);
20-
}
21-
getStyles(){
22-
var styles={
23-
screen512:null,
24-
biggerThan512:function(){
25-
if(!this.screen512){
26-
this.screen512=window.matchMedia(`(min-width: 512px)`)
27-
}
28-
return this.screen512.matches;
29-
},
30-
container:{
31-
display:"flex",
32-
flexDirection:"column",
33-
justifyContent:"flex-start",
34-
alignItems:"center",
35-
},
36-
qrCodeContainer:{
37-
padding:5,
38-
display:"flex",
39-
width:"100%",
40-
flexDirection:"column",
41-
justifyContent:"flex-start",
42-
alignItems:"center"
43-
},
44-
label:{
45-
marginTop:10,
46-
fontSize: 22,
47-
color: "#4880ED",
48-
fontWeight: 300,
49-
50-
}
51-
};
52-
return styles;
53-
54-
55-
}
56-
57-
58-
59-
render(){
60-
var styles=this.getStyles();
61-
var {code,size,level}=this.props;
62-
63-
if(!size){
64-
var w = window.innerWidth-50;
65-
var h = window.innerHeight-50;
66-
if(w<h){
67-
size=w;
68-
}
69-
else{
70-
size=h;
71-
}
72-
if(size>400){
73-
size=400;
74-
}
75-
}
76-
else{
77-
size=parseInt(size);
78-
}
79-
if(!level){
80-
level="H";
81-
}
82-
if(!code){
83-
code="";
84-
}
85-
return(
86-
<div style={styles.container}>
87-
<div style={styles.qrCodeContainer}>
88-
<QRCode
25+
};
26+
function computeDefaultSize(){
27+
console.log(window.innerWidth-50);
28+
console.log(window.innerHeight-50);
29+
let size = Math.min(window.innerWidth-50,window.innerHeight-50);
30+
return Math.max(size,400);
31+
}
32+
33+
export default ({label="",code="",level='H',size=0})=>{
34+
const [defaultSize,setDefaultSize]=useState(computeDefaultSize());
35+
useEffect(() => {
36+
function onWindowResize(){
37+
setDefaultSize(computeDefaultSize());
38+
}
39+
window.addEventListener("resize", onWindowResize);
40+
return ()=>{
41+
window.removeEventListener("resize", onWindowResize);
42+
}
43+
});
44+
return(
45+
<div style={styles.container}>
46+
<div style={styles.qrCodeContainer}>
47+
<QRCode
8948
value={code}
9049
level={level}
91-
size={size}
50+
size={size?size:defaultSize}
9251
/>
93-
</div>
94-
<div style={styles.label}>{this.props.label}</div>
95-
</div>
96-
);
52+
</div>
53+
<div style={styles.label}>{label}</div>
54+
</div>
55+
);
56+
};
9757

98-
}
99-
}

0 commit comments

Comments
 (0)