Skip to content

Commit eca3138

Browse files
authored
fix(lib): fix render on update (#5)
Add activity indication / loader while the qrcode is generated on initial and subsequent renders / re-renders. This removes the broken layout / render of the qrcode canvas from view and only displays it when qrcode is ready. #3
1 parent 9f097a7 commit eca3138

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/Canvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var createReactClass = require('create-react-class');
77
var {
88
View,
99
WebView,
10-
Platform
10+
Platform,
1111
} = require('react-native');
1212

1313
function getRenderHTML(context, render, generator) {

lib/QRCode.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Canvas = require('./Canvas.js');
77
var qr = require('qr.js');
88
var {
99
View,
10+
ActivityIndicator,
1011
} = require('react-native');
1112

1213
function renderCanvas(canvas) {
@@ -47,6 +48,10 @@ function renderCanvas(canvas) {
4748
});
4849
}
4950

51+
function getDiff(x, y) {
52+
return Object.keys(x).some(value => x[value] !== y[value]);
53+
}
54+
5055
var QRCode = createReactClass({
5156
PropTypes: {
5257
value: PropTypes.string,
@@ -70,6 +75,25 @@ var QRCode = createReactClass({
7075
}
7176
},
7277

78+
componentDidUpdate: function(prev) {
79+
const hasChanged = getDiff(prev, this.props);
80+
if(hasChanged) {
81+
this.setState({ isLoading: true });
82+
}
83+
},
84+
85+
getInitialState: function() {
86+
return { isLoading: true };
87+
},
88+
89+
onLoadEnd: function() {
90+
this.setState({
91+
isLoading: false,
92+
}, () => {
93+
this.props.onLoadEnd();
94+
});
95+
},
96+
7397
utf16to8: function(str) {
7498
var out, i, len, c;
7599
out = "";
@@ -93,8 +117,11 @@ var QRCode = createReactClass({
93117
render: function() {
94118
var size = this.props.size;
95119
var value = this.utf16to8(this.props.value);
120+
var styles = this.state.isLoading ? { width: 0, height: 0, overflow: 'hidden' } : undefined;
96121
return (
97-
<View>
122+
<>
123+
{ this.state.isLoading && <ActivityIndicator /> }
124+
<View style={styles}>
98125
<Canvas
99126
context={{
100127
size: size,
@@ -106,10 +133,11 @@ var QRCode = createReactClass({
106133
generateImage={this.props.getImageOnLoad}
107134
render={renderCanvas}
108135
onLoad={this.props.onLoad}
109-
onLoadEnd={this.props.onLoadEnd}
136+
onLoadEnd={this.onLoadEnd}
110137
style={{height: size, width: size}}
111138
/>
112139
</View>
140+
</>
113141
);
114142
}
115143
});

0 commit comments

Comments
 (0)