Skip to content

Commit d9e7433

Browse files
committed
create frag fix
1 parent ccbb6d4 commit d9e7433

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

example/src/App.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export default class App extends React.Component {
2020
style={{
2121
width: 900,
2222
height: 900,
23-
marginVertical: 20,
2423
}}
2524
onSuccess={(data) => {
2625
console.log("App BarCode On Success :", data);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-mlkit-barcode",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "test",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { createRef, PureComponent, useEffect, useRef, useState } from 'react';
2-
import { UIManager, findNodeHandle, NativeEventEmitter,View } from 'react-native';
2+
import { UIManager, findNodeHandle, NativeEventEmitter, View } from 'react-native';
33

44
import { MlkitBarcodeViewManager } from './MlkitBarcodeViewManager';
55

@@ -21,6 +21,8 @@ const destroyFragment = (viewId) =>
2121

2222
const eventEmitter = new NativeEventEmitter();
2323

24+
const TAG = "MlkitBarcodeView: ";
25+
2426
export class MlkitBarcodeView extends PureComponent {
2527

2628
constructor(props) {
@@ -32,31 +34,30 @@ export class MlkitBarcodeView extends PureComponent {
3234
}
3335

3436
componentDidMount() {
35-
if(this.props.enableQrScanner)
37+
if (this.props.enableQrScanner)
3638
this.startScanner();
3739
}
3840
componentWillUnmount() {
3941
// Here goes the code you wish to run on unmount
4042
// console.log("MlkitBarcodeView: Unmounting...");
41-
this.eventListener.remove();
43+
this.eventListener && this.eventListener.remove();
4244
// destroyFragment(viewId);
4345
}
4446

4547
componentWillReceiveProps(newProps) {
4648
this.props = newProps;
4749
// console.log("MlkitBarcodeView :enableQrScanner :" + this.props.enableQrScanner);
48-
if (this.props.enableQrScanner) {
49-
this.setState({enableQr:true},()=>{
50-
setTimeout(this.startScanner,400)
50+
if (this.props.enableQrScanner && !this.state.enableQr) {
51+
this.setState({ enableQr: true }, () => {
52+
setTimeout(this.startScanner, 400)
5153
})
52-
5354
}
54-
else {
55-
this.stopScanner();
55+
else if (this.state.enableQr) {
56+
this.stopScanner();
5657
}
5758
}
5859

59-
startScanner=()=>{
60+
startScanner = () => {
6061
this.eventListener = eventEmitter.addListener('BARCODE_SCANNED', (event) => {
6162
console.log(event) // "someValue"
6263
if (this.props.onSuccess)
@@ -68,22 +69,22 @@ export class MlkitBarcodeView extends PureComponent {
6869

6970
}
7071

71-
stopScanner=()=>{
72-
// this.eventListener.remove();
73-
const viewId = findNodeHandle(this.ref.current);
74-
destroyFragment(viewId);
75-
setTimeout(() =>this.setState({enableQr:false}), 400);
72+
stopScanner = () => {
73+
// this.eventListener.remove();
74+
const viewId = findNodeHandle(this.ref.current);
75+
destroyFragment(viewId);
76+
setTimeout(() => this.setState({ enableQr: false }), 400);
7677
}
7778

7879

7980
render() {
8081
if (this.state.enableQr) {
8182
return (
82-
<View style={{...this.props.style}}>
83-
<MlkitBarcodeViewManager
84-
{...this.props}
85-
ref={this.ref}
86-
/>
83+
<View style={{ position: 'absolute' }}>
84+
<MlkitBarcodeViewManager
85+
{...this.props}
86+
ref={this.ref}
87+
/>
8788
</View>
8889
);
8990
}

0 commit comments

Comments
 (0)