Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.

How to proper deactivate scanning if you are using react-navigation and go to next screen #136

Closed
nadzic opened this issue Sep 19, 2018 · 3 comments

Comments

@nadzic
Copy link

nadzic commented Sep 19, 2018

What's happening?

I would like to know the most proper way how to deactivate qr scanner if you go to next screen with react navigation

  • I tried with additional variable {qrScannerEnabled && <QRCode.. />}, that has performance issues - I just want enable/disable scanner, not always rerender the whole scanner.

  • I tried to set reactivate={qrScannerEnabled}, so to disable it when go to next screen, but it still scans code one more time, then it is disabled (I tried also force update with setState, but no effect)

Currently I am resetting the state with NavigationActions, but that is also not super performant.

Is there any smart solution for this?

Build details?

Android 8.1

@pkyeck
Copy link

pkyeck commented Sep 20, 2018

I'm using react-navigation's NavigationEvents:

import { NavigationEvents, NavigationScreenProps } from 'react-navigation';

// ...

onDidFocus = payload => {
  this.setState({ isFocused: true });
};

onDidBlur = payload => {
  this.setState({ isFocused: false });
};

render() {
  const { isFocused } = this.state;

  return (
    <View style={styles.container}>
      <NavigationEvents
        onDidFocus={this.onDidFocus}
        onDidBlur={this.onDidBlur}
      />
      {isFocused && (
        <QRCodeScanner />
      )}
    </View>
  );
}

But this is also rerendering on every page-switch. If you find a better solution, let me know.

@superp
Copy link

superp commented May 20, 2019

If you still have problem with onDidFocus not firing, maybe you have the same:
react-navigation/redux-helpers#53

mnzaki added a commit to jolocom/smartwallet-app that referenced this issue Jul 15, 2019
infinitered/ignite-andross#277
moaazsidat/react-native-qrcode-scanner#161
moaazsidat/react-native-qrcode-scanner#177
moaazsidat/react-native-qrcode-scanner#136
react-native-camera/react-native-camera#1797
react-native-camera/react-native-camera#1686
react-native-camera/react-native-camera#1686
react-navigation/redux-helpers#87
react-navigation/redux-helpers#60
react-navigation/redux-helpers#53
https://www.youtube.com/watch?v=CnQ8N1KacJc

Fixes #1326

The navigation actions were turned into ThunkActions and now directly
call react-navigation's navigator.dispatch, which handles state
internally

Usages of the navigationActions were fixed

Also QRcodeScanner was cleaned up a bit and hacked to properly re-enable
the camera by re-rendering it (so it gets remounted). Now camera works
again if you press back after scanning a QR code.
@cs-manughian
Copy link

cs-manughian commented Sep 23, 2021

Hi, set to de/reactivate the scanner by hooking into the focus lifecycle from react-navigation (this is typescript with react native):


    import { useFocusEffect, useIsFocused } from '@react-navigation/native';

    // Inside your component:
    
    // Use this to determine the focus state of the screen
    const isFocused = useIsFocused();
    
    // Alternatively, hook into the focus lifecycle method
    const [isReactivated, setIsReactivated] = useState<boolean>(true);
    useFocusEffect(
        useCallback(() => {
            // Do something when the screen is focused
            // Add some custom logic here...
            setIsReactivated(true);
            return () => {
                // Do something when the screen is unfocused
                setIsReactivated(false);
            };
        }, [])
    );
    
    return (
           isFocused && 
            <QRCodeScanner
                reactivate={isReactivated} // Or, you can use isFocused here
                ... />
    )
    ...

References:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants