| Conditions | 4 |
| Total Lines | 60 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React, { useState, useEffect } from 'react'; |
||
| 5 | |||
| 6 | export default function QrScanner({navigation, cameraVisible, setCameraVisible}) { |
||
| 7 | const [hasPermission, setHasPermission] = useState(null); |
||
| 8 | const [scanned, setScanned] = useState(false); |
||
| 9 | |||
| 10 | useEffect(() => { |
||
| 11 | const getBarCodeScannerPermissions = async () => { |
||
| 12 | const { status } = await BarCodeScanner.requestPermissionsAsync(); |
||
| 13 | setHasPermission(status === 'granted'); |
||
| 14 | }; |
||
| 15 | |||
| 16 | getBarCodeScannerPermissions(); |
||
| 17 | }, []); |
||
| 18 | |||
| 19 | const handleBarCodeScanned = ({ type, data }) => { |
||
| 20 | setScanned(true); |
||
| 21 | alert(`Bar code with type ${type} and data ${data} has been scanned!`); |
||
| 22 | }; |
||
| 23 | |||
| 24 | if (hasPermission === null) { |
||
| 25 | return <Text>Requesting for camera permission</Text>; |
||
| 26 | } |
||
| 27 | if (hasPermission === false) { |
||
| 28 | return <Text>No access to camera</Text>; |
||
| 29 | } |
||
| 30 | |||
| 31 | return ( |
||
| 32 | <Modal |
||
| 33 | animationType="slide" |
||
| 34 | transparent={true} |
||
| 35 | visible={cameraVisible} |
||
| 36 | onRequestClose={() => { |
||
| 37 | setCameraVisible(!cameraVisible); |
||
| 38 | }} |
||
| 39 | > |
||
| 40 | <View style={[styles.container, styles.shadowProp]}> |
||
| 41 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => setCameraVisible(!cameraVisible)}> |
||
| 42 | <Icon |
||
| 43 | name='x' |
||
| 44 | size={25} |
||
| 45 | color='black' |
||
| 46 | /> |
||
| 47 | </Pressable> |
||
| 48 | |||
| 49 | <Text style={styles.title}> Scan the QR-code </Text> |
||
| 50 | |||
| 51 | <BarCodeScanner |
||
| 52 | onBarCodeScanned={scanned ? undefined : handleBarCodeScanned} |
||
| 53 | style={styles.viewFinder} |
||
| 54 | /> |
||
| 55 | |||
| 56 | <Pressable style={[styles.cameraButton, styles.shadowProp]} onPress={() => setScanned(false)} > |
||
| 57 | <Icon |
||
| 58 | name='screen-full' |
||
| 59 | size={24} |
||
| 60 | color='black' |
||
| 61 | /> |
||
| 62 | </Pressable> |
||
| 63 | </View> |
||
| 64 | </Modal> |
||
| 65 | |||
| 127 | }) |