【问题标题】:React native app, pressing android-phone back-button should refresh previous page or component?反应本机应用程序,按android-phone后退按钮应该刷新上一个页面或组件?
【发布时间】:2021-04-15 07:46:05
【问题描述】:

我在我的 react-native 应用程序中使用 react-navigation 5。我有两个名为 A 和 B 的组件(例如)。从 A 组件我可以转到 B 组件,从 B 组件我可以转到 A 组件并可以刷新组件 A。但是当我按下 android-system 后退按钮时,我无法刷新以前的组件(A)。 /p>

示例代码:

Class A extend React.component{
   componentdidmount = () => {
      //some api call
   }
render(){
   return(
     <Button onPress={this.props.navigation.push("B")}> Goto B Component</Button>
   );
}
}

Class B extend React.component{
   componentdidmount = () => {
      //some api call
   }
render(){
   return(
     <Button onPress={this.props.navigation.push("A")}> Goto A Component</Button>
   );
}
}

当android-system后退按钮被按下时如何刷新之前的组件?谢谢

【问题讨论】:

  • 嗨,stackoverflow.com/questions/44223727/…你也有同样的问题,祝你好运
  • 使用 android 系统导航按钮(适用于每部 android 手机)我想更新以前的组件。提供的链接没有帮助。

标签: javascript reactjs react-native react-navigation


【解决方案1】:

试试这个

import { BackHandler } from "react-native";         

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

componentWillUnmount()
  {BackHandler.removeEventListener('hardwareBackPress',this.handleBackButtonClick);}    
    
handleBackButtonClick = () => {this.props.navigation.push("B"); return true;};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多