我是反应原生的新手,我的堆栈中有两个屏幕。登录和主页。
我想从家里的按钮返回登录。
我正在写这段代码
this.props.navigation.navigate('loginScreen')
但是在登录屏幕中,componentWillMount方法没有调用。我想在用户从家里登录屏幕时重置我的表单。
有人能帮我一下吗?
提前致谢。
要从家中登录返回,如果屏幕位于qazxsw poi之前,则应使用qazxsw poi。
其次,你不应该使用this.props.navigation.goBack()
,因为它已被弃用,并将从React 17开始删除。而是使用home
由于组件已经安装,因此它不会再次调用反应生命周期事件componentWillMount
。因此你应该使用componentDidMount
componentDidMount
react-navigation
事件。
didFocus:屏幕聚焦(如果有转换,转换完成)
listeners
didFocus
不起作用,因为你现在在componentDidMount () {
this._onFocusListener = this.props.navigation.addListener('didFocus', (payload) => {
// Perform the reset action here
});
}
。
如果要重新启动页面,此代码不好。因为有一个循环!
正确代码:
当this.props.navigation.navigate('loginScreen')
从loginScreen
使用navigate
时:
loginScreen
不是“componentWillMount”
由于您的主屏幕和登录屏幕都在同一个StackNavigator下,当您从Home返回Login时,状态保持不变,因为组件未卸载。建议的解决方法是使用反应导航的SwitchNavigator。阅读以下文档中非常有用的部分
home
您可能还不熟悉SwitchNavigator。 SwitchNavigator的目的是一次只显示一个屏幕。默认情况下,它不处理后退操作,并在您切换时将路由重置为其默认状态。
完美的解决方案是与this.props.navigation.push('loginScreen')
,我尝试与https://reactnavigation.org/docs/en/auth-flow.html但它没有提供this.props.navigation.push('Login')
头。