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

146
Views
Not able to perform 2 actions on the same component when hardware back button is pressed in react-native,below are the code regarding the backhandler
 backAction = () => {
    if (this.props.navigation.isFocused() && !this.state.otpScreen) {
     
  this.setState({ showLoginScreen: true });
  return true;

} else if(this.state.showLoginScreen) {
  this.props.navigation.dispatch(CommonActions.reset({
    index: 0,
    routes: [
      { name: 'Login' },
    ],
  }))
  
  return false;
}

};

the code above is for action that should be done by the hardware back press, at first it should show the one screen and on the second press it should exit the app, both otp screen and login screen are on the same component, I'm just hiding based on the condition ... at the first time it is working as per the condition but for the second time it's not.

 componentDidMount() {
this.focusListener = this.props.navigation.addListener('focus', () => {
  this.setState({
    mobile: '',
    showLoginScreen: true,
  });
});
this.getDataFromStorage();
AsyncStorage.setItem('countryCode', '+91');
BackHandler.addEventListener(
  "hardwareBackPress",
  this.backAction
);


}
  componentWillUnmount() {
    clearInterval(this.interval)
    BackHandler.addEventListener('hardwareBackPress', () => { return false })
  }

can anyone pls help me how to do this,thanks in Advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I guess your problem is that you can't access the 'current' value of a state inside a listener.

More Details here

Try to use a Reference instead of a state

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!