Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
Adding an if statement to a Stack Navigatior in React Native

I have a "chat" screen that can be accessed from multiple screens in my React Native App. I am trying to add an "if statement" with a "route.params?" to the stack navigator, so it will navigate the user back to the proper screen, depending on where they are coming from, but I cannot get the "if statement" to execute.

Below is my code:

    function MainTabScreen ({ navigation, route }){
      
      const didComeFromNextScreen = () => route.params
      const didComeFromOtherScreen = () => route.params
      
    
      const chatChecker = ()=>{
    
        if(route.params?.didComeFromNextScreen){
          console.log('It came from NextScreen')
          navigation.navigate('NextScreen')
        return;
        }
    
        if(route.params?.didComeFromOtherScreen){
          console.log('It came from OtherScreen')
          navigation.navigate('OtherScreen')
        return;
        }
    
      }
 
      <MainTab.Navigator screenOptions ={{
              headerStyle:{
                  backgroundColor:"#fff" ,        
                },
                gestureEnabled: false,
                headerTintColor: 'red', 
                headerTitleStyle: {
                  fontWeight: 'bold',
                  fontSize: height*0.023,
                  textAlign: 'center',
                }
            }}
       >
    
    
        <MainTab.Screen name = "NextScreen" component={NextScreen} options= {{
                title: '' ,
                headerTintColor: '#fff',
                headerLeft: null,
                headerRight: () => (
                  <Icon.Button name = "message" size={25} backgroundColor= "#fff" onPress={ () => navigation.navigate('ChatScreen',{
                  didComeFromNextScreen: true
                })}> 
        </Icon.Button>
              }}
    />
        
        <MainTab.Screen name = "ChatScreen" component={ChatScreen} options= {{
                title: '' ,
                headerTintColor: '#fff',
                headerLeft: ()=>(
                <Icon.Button name = "arrow-left" size={25} backgroundColor= "#fff" onPress={chatChecker}/>          
                )
              }}
    />
    
        <MainTab.Screen name = "OtherScreen" component={OtherScreen} options= {{
                title: '' ,
                headerTintColor: '#fff',
                headerLeft: null,
                headerRight: () => (
onPress={ () => navigation.navigate('ChatScreen',{
                  didComeFromOtherScreen: true
                })}> 

              }}
    />
        
        </MainTab.Navigator>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I am not sure why you made these functions but functions will always be true so it wont work

const didComeFromNextScreen = () => route.params
const didComeFromOtherScreen = () => route.params

update to

const didComeFromNextScreen = route.params
const didComeFromOtherScreen = route.params
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda