Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
How can I add an Alert on StackScreen

I have a question about this code, I want to add an Alert if you press the BackButton but only in 2 navigation screen (MARKETPLACE and SHOP) The Alert is to confirm is you want to back or cancel if not How can I do that? My BackButton is the first Stack.Screen This is the code:

<Stack.Navigator>
<Stack.Screen
  name='Root'
  component={DrawerNavigator}
  options={{
  headerShown: false,
  title: '',
  headerBackTitle: 'BackButton
  }}/>
      <Stack.Screen options={{ title: 'MARKETPLACE' }} name='Marketplace' component={Marketplace} />
      <Stack.Screen options={{ title: 'SHOP' }} name='Shop' component={Shopping} />
      <Stack.Screen options={{ title: 'CHECKOUT' }} name='Checkout' component={Checkout} />
      <Stack.Screen options={{ title: 'CHECK' }} name='Check' component={CheckSuccess} />
</Stack.Navigator>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Starting from @react-navigation/native version 5.7+ you can add beforeRemove listener to the navigation object to prevent going back.

EXAMPLE

function Marketplace({ navigation }) {
  const [text, setText] = React.useState('');
  const hasUnsavedChanges = Boolean(text);

  React.useEffect(
    () =>
      navigation.addListener('beforeRemove', (e) => {
        if (!hasUnsavedChanges) {
          // If we don't have unsaved changes, then we don't need to do anything
          return;
        }

        // Prevent default behavior of leaving the screen
        e.preventDefault();

        // Prompt the user before leaving the screen
        Alert.alert(
          'Discard changes?',
          'You have unsaved changes. Are you sure to discard them and leave the screen?',
          [
            { text: "Don't leave", style: 'cancel', onPress: () => {} },
            {
              text: 'Discard',
              style: 'destructive',
              // If the user confirmed, then we dispatch the action we blocked earlier
              // This will continue the action that had triggered the removal of the screen
              onPress: () => navigation.dispatch(e.data.action),
            },
          ]
        );
      }),
    [navigation, hasUnsavedChanges]
  );

  return (
    <TextInput
      value={text}
      placeholder="Type something…"
      onChangeText={setText}
    />
  );
}

Check more Details

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda