代码之家  ›  专栏  ›  技术社区  ›  Mayank Jain Pushpendra Rathor

React本机组件willmount不调用

  •  2
  • Mayank Jain Pushpendra Rathor  · 技术社区  · 6 年前

    我正在写这个代码

    this.props.navigation.navigate('loginScreen')
    

    但在登录界面 方法未调用。当用户从主页进入登录屏幕时,我想重置我的窗体。

    有人能帮我吗?

    提前谢谢。

    4 回复  |  直到 6 年前
        1
  •  1
  •   Pritish Vaidya    6 年前

    要从主页登录返回,您应该使用 this.props.navigation.goBack() 如果屏幕在 home .

    其次,你不应该使用 componentWillMount componentDidMount

    因为组件已经挂载,所以它不会调用react生命周期事件 组件安装 再一次。因此,您应该使用 react-navigation listeners didFocus

    didFocus公司 :屏幕聚焦(如果有转换,转换完成)

    componentDidMount () {
        this._onFocusListener = this.props.navigation.addListener('didFocus', (payload) => {
          // Perform the reset action here
        });
    }
    
        2
  •  2
  •   morteza ataiy    6 年前

    这个 this.props.navigation.navigate('loginScreen') 不要工作,因为你现在在工作 loginScreen .

    有一个循环!

    正确代码:

    什么时候 navigate 登录屏幕 home 用途:

    this.props.navigation.push('loginScreen')
    

    不在“componentWillMount”中

        3
  •  0
  •   needsleep    6 年前

    因为主屏幕和登录屏幕都在同一个StackNavigator下,所以当您从主屏幕返回登录时,状态保持不变,因为组件没有卸载。解决这个问题的推荐方法是使用react navigation的SwitchNavigator。请阅读下面文档中非常有用的部分

    https://reactnavigation.org/docs/en/auth-flow.html

    您可能还不熟悉SwitchNavigator。目的 SwitchNavigator一次只能显示一个屏幕。默认情况下, 它不处理返回操作,并将路由重置为其默认值 当你离开的时候说出来。

        4
  •  0
  •   Andronicus    6 年前

    this.props.navigation.push('Login') ,我试过了 SwitchNavigator 但它不能提供 navigationOptions 用于标题。