question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I cannot use react navigator in the onRead function

See original GitHub issue

What’s happening?

I am trying to navigate the user to a different screen when onRead is successful. But throws an error saying TypeError: undefined is not an object (evaluating 'navigation.navigate')

How can it be reproduced?

Try to use react-navigation in the function of onRead to reproduce this error.

Build details?

I am using the latest version of react-native-qrcode-scanner on a physical device running android.

My code

import React from 'react';
import QRCodeScanner from 'react-native-qrcode-scanner';
import {RNCamera} from 'react-native-camera';
import {Text, View} from 'react-native';
import {globalStyles} from '../styles/global';
import {useDispatch} from 'react-redux';
import {startUsing} from '../redux/actions/deskActions';

const Start = ({navigation}) => {
  const dispatch = useDispatch();
  const onSuccess = e => {
    // dispatch(startUsing(e.data));
    console.log(e.data);
    navigation.navigate('Home');
    scanner.reactivate();
  };

  return (
    <View style={globalStyles.container}>
      <View style={{...globalStyles.center, marginVertical: 50}}>
        <Text style={{...globalStyles.titleText, marginBottom: 20}}>
          Scan the QR Code to start session
        </Text>
        <QRCodeScanner
          reactivate={true}
          reactivateTimeout={2000}
          showMarker={true}
          ref={node => {
            scanner = node;
          }}
          onRead={onSuccess}
          flashMode={RNCamera.Constants.FlashMode.auto}
          cameraStyle={{width: '80%', marginHorizontal: 20, height: '100%'}}
        />
      </View>
    </View>
  );
};

export default Start;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
manan2501commented, Mar 27, 2021

if you’re using react-navigation v5 you can try using hook

Yay! This is working as expected. Thanks a lot for helping. 😄

0reactions
hansderlycommented, Mar 27, 2021

Glad that help you! Happy Coding @manan2501 !

Read more comments on GitHub >

github_iconTop Results From Across the Web

onReady={() => {}} callback called before navigationRef is ...
I am following the react native screen navigation documentation and passing a callback to onReady to set the routeNameRef.current to the ...
Read more >
Cannot read property 'navigate' of undefined React Native - ...
I've solved the problem using useNavigation function: import { View, Button } from 'react-native'; import { useNavigation } from ...
Read more >
NavigationContainer
The NavigationContainer is responsible for managing your app state and linking your top-level navigator to the app environment.
Read more >
React Native
Sentry's React Native SDK enables automatic reporting of errors and exceptions, and identifies performance issues in your application. The React Native SDK ...
Read more >
NavigationContainer | React Navigation - Netlify
It's also possible to attach a ref to the container to get access to various helper methods, for example, dispatch navigation actions. Example:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found