react-native - this.props.navigation.navigate 不导航到其他屏幕
问题描述
我有一个标签导航器和堆栈导航器。这是结构;
const MainNavigator = createBottomTabNavigator({
welcome: { screen: WelcomeScreen },
auth: {
screen: createStackNavigator({
auth: {
screen: AuthScreen,
navigationOptions: {
header: null
}
},
login: {
screen: LoginScreen,
navigationOptions: {
title: 'Login',
headerTintColor: '#fff',
headerStyle: {
backgroundColor: '#3f2141'
}
}
},
newAccount: {
screen: NewAccountScreen,
navigationOptions: {
title: 'Create an Account',
headerTintColor: '#fff',
headerStyle: {
backgroundColor: '#3f2141'
},
headerTitleStyle: {
fontSize: 20
}
}
}
}),
},
main: { screen: MainScreen }
})
尝试从身份验证屏幕传递到登录屏幕或新帐户屏幕时没有问题。但是如果创建帐户成功,我会尝试从新帐户屏幕传递到主屏幕。这是尝试执行此操作的操作代码;
export const createUser = ({ email, password }) => {
return(dispatch) => {
dispatch({ type: CREATE_USER });
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(user => createUserSuccess(dispatch, user))
.catch(() => createUserFail(dispatch));
};
};
const createUserFail = (dispatch) => {
dispatch({ type: CREATE_USER_FAIL })
};
const createUserSuccess = (dispatch, user) => {
dispatch({
type: CREATE_USER_SUCCESS,
payload: user
});
navigateToMainScreen();
};
const navigateToMainScreen = () => {
this.props.navigation.navigate('main')
};
但是导航功能在这里不起作用,因此用户创建帐户但屏幕不会改变。我想通了,可能是因为一个新帐户屏幕在堆栈导航器中,而主屏幕在选项卡导航器中,但我无法解决问题。
解决方案
If you're trying to call this.props.navigation.navigate
in a separate component, the navigation
prop must be passed to it first. Let's say you want to use navigation
in NewAccountScreen
then pass the prop like this:
<NewAccountScreen navigation={this.props.navigation}/>
in the parent component where the NewAccountScreen
needs to be rendered.
Another way to use navigation
anywhere in your app is using withNavigation
. Following the official documentation:
import React from 'react';
import { Button } from 'react-native';
import { withNavigation } from 'react-navigation';
class NewAccountScreen extends React.Component {
// your code including the call to this.props.navigation.navigate()
// ...
}
// withNavigation returns a component that wraps NewAccountScreen
// and passes in the navigation prop:
export default withNavigation(NewAccountScreen);
Using this approach, you can render NewAccountScreen anywhere in your app without passing in a navigation prop explicitly and it will work as expected.
On a side note, I highly recommend you to rethink your router structure. You may have your reasons but putting the auth and main screens in the same createBottomTabNavigator
doesn't make much sense to me. There's official documentation regarding authentication flows using createSwitchNavigator
which will make your life easier. Consider giving it a shot before your project becomes more complicated!
推荐阅读
- arrays - 如何从具有不同索引的 Laravel 对象数组中获取 2 个最高值
- sql - 如何根据分配的“ID”将表数据透视到 SQL 中的值?
- javascript - 使用 fetch API javascript 时无法从数组中获取数据
- javascript - 如何在不重写的情况下将标题添加到不同的页面
- python - 再次调用函数时对列表的更改保持不变(不需要)
- c# - Convert.ChangeType 不接受 Decimal 的“1E-5”科学记数法,如何解决?
- makefile - GNU MakeFile 包括另一个带有 eval 函数的 makefile 简单问题
- r - R:ties.method = random:使用 ties.method = random 排名时如何始终获得相同的随机化
- c# - 如何在 ASP.NET Core 2.1 上的另一个 API 中调用 API
- princexml - 如何使用 PrinceXML 将 div 对齐到 PDF 中页面的底部