Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  154] [ 6]  / answers: 1 / hits: 5452  / 2 Years ago, fri, january 28, 2022, 12:00:00

I am new to react native and react navigation and I get this error.



Uncaught TypeError: Cannot read properties of undefined (reading 'navigate')



I don't understand my because I am just using the same code which already worked, but now it's not working.


I am trying to make a back arrow that send you back to the MachineList screen.


//doesn't work
const AddMachineDetails = ({route}, props) => {

...

<TouchableOpacity onPress={() => props.navigation.navigate("MachinesList")}>
<BackArrow />
</TouchableOpacity>

So when I press my backarrow it send me this error:



Uncaught TypeError: Cannot read properties of undefined (reading 'navigate')



Here is my Navigation file:


import MachinesList from '../components/MachinesList'
import AddMachineDetails from '../components/AddMachineDetails';

...

function MachineListStackScreen() {
return(
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name='MachinesList' component={MachinesList}/>
<Stack.Screen name='AddMachineDetails' component={AddMachineDetails}/>
</Stack.Navigator>
)
}

...

I think the problem is somewhere in this code, but if not, I'll show you more of my code


Here is an example where my navigation.navigate work perfectly:


//no specific imports  (but works)

const MachinesList = (props) => {

...


<TouchableOpacity style={styles.machineBox} onPress={() => props.navigation.navigate('AddMachineDetails', {item})}>

More From » reactjs

 Answers
5

You have to desctruct the props argument.


const AddMachineDetails = ({route, ...props}) => { ... }  

or


const AddMachineDetails = ({route, navigation, ...props}) => { ... }  // then straightly use the *navigation*

[#431] Saturday, January 22, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
hallie questions
;