Passing data between pages using React Native hooks

I am a newcomer to React Native and facing challenges in passing data to another page. Specifically, I want to transmit data from the QR Reader to another Page.

Below is my code on the first screen:

const LoginScreen = (props) => { 

    const onSuccess = e => {
        const Data = e.data
        console.log(Data);
        props.navigation.replace("Input Water Transaction", {result: Data })
    };

    return (
        <QRCodeScanner
            onRead={onSuccess}
            flashMode={RNCamera.Constants.FlashMode.off}
            topContent={
                <Text>
                    Scan QR Water
                </Text>
            }
            bottomContent={
                <TouchableOpacity style={styles.buttonTouchable}>
                    <Text style={styles.buttonText}>Scan QR Water</Text>
                </TouchableOpacity>
            }
        />
    );
}

This is the second screen:

const InputWater = (props) => {

    const navigation = useNavigation();   
    
    const QRResult =  props.navigation.getParam('result', 'nothing sent')
    return (

        <ScrollView>
            <View style={{
                flex:1,
    
            }}>

                <Root>
                    <TouchableOpacity
                        onPress={() =>
                            Popup.show({
                                type: 'Success',
                                title: 'Upload complete',
                                button: true,
                                textBody: 'Congrats! Your upload was successful!',
                                buttontext: 'Ok',
                                callback: () => navigation.navigate('Scan QR Water')
                            })
                        }
                    >
                        
                        <View style={{
                            backgroundColor: '#4cd137',
                            marginTop: 10,
                            borderRadius: 10,
                        }}>
                        
                            <Text style={{
                                marginTop: 10,
                                marginBottom: 10,
                                fontWeight: 'bold',
                                textAlign: 'center'
                            }}>{QRResult}</Text>
                        </View>
                    
                    </TouchableOpacity>
                </Root>

                <View style={{
                    flexDirection: 'column',
                    justifyContent: 'flex-start',
                    alignItems: 'center',
                    alignContent: 'center',
                    marginTop: 10,
                    
                }}>

            
        
                    <View style={{
                        width: 350,
                        minWidth: 310,
                        maxWidth: 310, 
                        backgroundColor: '#d6e0f0',
                        marginTop: 10,
                        borderRadius: 10,
                        alignItems: 'stretch',
                    }}>
                        <View style={{
                            backgroundColor: '#fff',
                            marginTop: 5,
                            marginLeft: 5,
                            marginRight: 5,
                            borderRadius: 10,
                            marginBottom: 5
                        }}>
                            <View style={{
                                marginTop: 10,
                                marginBottom: 10,
                                marginLeft: 10,
                                flexDirection: 'column'
                            }}>
                                <Text>Electricity Meter No</Text>
                                <TextInput style={{
                                    backgroundColor: '#ecf0f1',
                                    borderRadius: 10,
                                    height: 35,
                                    minWidth: 240,
                                    maxWidth: 280,
                                    marginTop: 5,
                                }}/>
        
                                <Text style={{
                                    marginTop: 10
                                }}>Start Electricity Meter</Text>
                                <TextInput style={{
                                    backgroundColor: '#ecf0f1',
                                    borderRadius: 10,
                                    height: 35,
                                    minWidth: 240,
                                    maxWidth: 280,
                                    marginTop: 5,
                                }}/>
        
                                <Text style={{
                                    marginTop: 10
                                }}>Last Electricity Meter</Text>
                                <TextInput style={{
                                    backgroundColor: '#ecf0f1',
                                    borderRadius: 10,
                                    height: 35,
                                    minWidth: 240,
                                    maxWidth: 280,
                                    marginTop: 5,
                                }}/>


                                <Text style={{
                                    marginTop: 10
                                }}>Lost Electricity(%)</Text>
                                <TextInput style={{
                                    backgroundColor: '#ecf0f1',
                                    borderRadius: 10,
                                    height: 35,
                                    minWidth: 240,
                                    maxWidth: 280,
                                    marginTop: 5,
                                }}/>

                            </View>
                        </View>
                   
                    </View>
                        
                    <View style={{
                        justifyContent: 'center',
                        flexDirection: 'column',
                        alignItems: 'center',
                        marginTop: 200
                        
                    }}>
                        <Text style={{
                            fontSize: 20,
                            fontWeight: 'bold',
                            textAlign: 'center'
                        }}>Water Transaction</Text>
                    </View>
                </View>
            </View>
        </ScrollView>
    
    );
};

However, an error occurred:

ERROR    TypeError: props.navigation.getParam is not a function. (In 'props.navigation.getParam('result', 'nothing sent')', 'props.navigation.getParam' is undefined)

How can I retrieve values from the first screen? Am I doing something wrong here? Any suggestions are much appreciated. Thank you!

Answer №1

When working with React Navigation 5, access parameters using props.route.params

For older versions, use props.navigation.state.params instead

Make sure to pass parameters when navigating with navigation.navigate('ScreenName', {params})

The behavior with navigation.replace is not confirmed

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method to retrieve the local filepath from a file input using Javascript in Internet Explorer 9?

I'm experiencing some issues with the image-preview function on IE9, similar to the example photo shown. This method is not functioning properly and throwing an error in IE9, while it works perfectly fine in IE6-8. I am looking for a way to retrieve ...

Inquiring about socket.io: How can an io emit its own signal?

I am currently working on implementing the emit event in an express router, and I'm attempting to pass a global.io variable. However, I've encountered an issue where despite adding the following code: io.emit('join','Tudis' ...

Experiencing a problem with the API code in Android Studio

Having some trouble with my code. I'm using JSONParser to read data, but I keep getting an error message indicating that I need a try/catch clause. However, when I add it, the entire code gets underlined and another error appears related to my API cod ...

Removing the navigation button from the hamburger menu

I am working on creating a semi-progressive top navigation bar. For the mobile viewport, the navigation bar will only display the logo and a hamburger button. When the button is clicked, various navigation menu options as well as the Log In and Sign Up bu ...

What are some ways to keep text within the boundaries of a div element?

I have tried multiple solutions for this issue, but none seem to be working for me. When I append a paragraph to a div, the text extends beyond the element. Below is the code I am using. Any assistance would be greatly appreciated. CSS: .chat-h { margi ...

Exploring the differences between arrays and filter functions within MongoDB aggregation operations

Recently, I crafted a sophisticated pipeline for my database: let orders = await Order.aggregate( { $unwind: "$candidate", }, { $lookup: { from: "groups", localField: & ...

I need guidance on how to successfully upload an image to Firebase storage with the Firebase Admin SDK

While working with Next.js, I encountered an issue when trying to upload an image to Firebase storage. Despite my efforts, I encountered just one error along the way. Initialization of Firebase Admin SDK // firebase.js import * as admin from "firebas ...

After clearing the option, the onChange function stops functioning

I'm facing an issue with the following code: success: function (data) { $('#' + idDivRefresh).endLoading(); if (data.message != '@Geral.Sucesso') { $('#' + idDropDown + ...

What is the method for graphing data points that include two different y values?

I have been on the lookout for a solution to this question for quite some time now. While there are a few options that come close, I am yet to find the perfect answer. My goal is to create a line chart displaying water levels over time. I would like the d ...

Using Regular Expressions to Verify Numerous Postal Codes within an Array

I'm currently working on a function that validates postcodes using a regex. The function should be able to handle either a single postcode or multiple postcodes. When I input a single postcode, everything works as expected. However, when I try to inp ...

Angular template driven forms fail to bind to model data

In an attempt to connect the model in angular template-driven forms, I have created a model class and utilized it to fill the input field. HTML: <div class="form-group col-md-2 col-12" [class.text-danger]="nameCode.invalid && nameCode.touched ...

Disabling a CountDownTimer when the Physical Back Button is Pressed within a Fragment

Hey there, I've been struggling to stop a CountDownTimer in a Fragment when the user presses the back button on their phone. I've tried overriding onPause, onDestroy, onStop, and onDestroyView but so far nothing has worked for me. Feeling a bit l ...

Top recommendation for determining the average value

Currently, I am in the process of developing an application that displays the frequency response of an audio signal within the 1/3 octave range. Using a for loop, I am able to retrieve the frequency for each sample. I have implemented if statements to val ...

Transform Text into Numeric Value/Date or Null if Text is Invalid

Consider the TypeScript interface below: export interface Model { numberValue: number; dateValue: Date; } I have initialized instances of this interface by setting the properties to empty strings: let model1: Model = { numberValue: +'', ...

What is the best way to address the "in fragment" issue in Android Studio?

bottomNavigationView = findViewById(R.id.bottom_navigation); getSupportFragmentManager().beginTransaction().replace(R.id.container, homeFragment).commit(); bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedL ...

jQuery does not cache Ajax requests by default

I have implemented the following code to make an AJAX request. I am trying to determine if the requests are being cached by using the Chrome developer tools. However, when I check the request tab, I notice that all data is always being pulled from the serv ...

Which JavaScript framework tackles the challenges of managing asynchronous flow, callbacks, and closures?

I have a strong aversion to JavaScript. I find it to be quite messy and disorganized as a language. However, I recognize that my lack of proficiency in coding with it may contribute to this perception. These past few days have been incredibly frustrating ...

Getting a multitude of contacts exceeding 1000 with ngCordova in AngularJS

I am currently working on an app using the Ionic framework that allows users to view and select contacts from their device. I am utilizing ngCordova's $cordovaContacts module to retrieve the contacts. Here is the service code responsible for fetching ...

Transforming a two-column text document into a set of key-value pairs

Recently, I've been diving into the world of Node.js and Express. My current challenge involves converting a text file into key-value pairs. The approach I'm taking with my staircase program is as follows: https://i.sstatic.net/GPs200IQ.png Th ...

Is there a feature similar to Nuxt.js' auto-register in Next.js?

My Journey as a Beginner Being a beginner in the tech world, specifically in full-stack development (although I'm about 8 years behind), I find myself grappling with the decision of what to focus on learning. Vue and Nuxt.js are fantastic technologi ...