Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
¿Cómo puedo agregar una alerta en StackScreen?

Tengo una pregunta sobre este código, quiero agregar una alerta si presiona el botón Atrás pero solo en 2 pantallas de navegación (MARKETPLACE y SHOP) La alerta es para confirmar si desea retroceder o cancelar si no ¿Cómo puedo hacer eso? Mi BackButton es la primera Stack.Screen Este es el código:

 <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 answers
Answer question

0

A partir de @react-navigation/native versión 5.7+ , puede agregar beforeRemove listener al objeto de navegación para evitar volver atrás.

EJEMPLO

 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} /> ); }

Ver más Detalles

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!