How to close one screen (navigation) in React Native, from another

Asked

Viewed 1,102 times

0

I need to know how I close the canvases I’ve already opened behind. Like: I’m going A - B - C - D ...when I get to D, I want to go back to A. But, if I press the button to go back on both Android and iOS, it will leave the app, and not come back D - C - B - A to then leave. I’m using Navigation to move between screens.

1 answer

0

Oops, all right?

If case you are using the library React-Navigation, for you to achieve this, you can manage the "Back" of Android using a Backhandler.

Example:

import React from 'react';
import {..., BackHandler, ... } from 'react-native';
export default class Main extends React.Component {
// ...
  exemploDeHandler(){
  //Faça Alguma coisa;
  if (!this.onMainScreen()) {
    this.goBack();
    return true;
  }
  return false;

  // Retornar true no Handler,
  // Segundo a documentação(Link logo Abaixo do Código)
  // evita a execução do comportamento default
  }

  componentDidMount(){
    BackHandler.addEventListener('hardwareBackPress', this.ExemploDeHandler);
  }

  componentWillUnmount(){
    BackHandler.removeEventListener('hardwareBackPress', this.ExemploDeHandler);
  }
// ...
}

Documentation: Backhandler

Browser other questions tagged

You are not signed in. Login or sign up in order to post.