无法导航到子屏幕
Can not able to navigate to child screens
我的 initialRouteName 是 loginScreen。
登录后第二个屏幕出现 BottamTabNavigation。 BottomTab 包含 4 个屏幕,其中一个名为 GroupScreen 的屏幕转到 TopTabScren。我无法导航至此屏幕。
流程是 --> loginScreen --> BottomTabScreen --> TopTabScreen。
我无法导航到 TopTabScreen。它给出了错误
"undefined is not an
object(evaluating'this.props.navigation.navigate' ".
TopTabScreen 导致进一步的 4 个屏幕
但是当我设置 InitailRoutName= "TobTabScreen" 时,所有四个屏幕都可以使用它。
对于导航屏幕,我正在使用这个。
onPress={() => this.props.navigation.navigate('screenName')}.
获取底部标签栏屏幕导航道具
_getScreenProps = () => {
return (
{
navigation: this.props.navigation,
}
)
}
渲染标签栏
render() {
return (
<View style={{ flex: 1.0 }}>
<Stack screenProps={this._getScreenProps()} />
</View>
)
}
在 Tab 屏幕上使用导航如下
onPress={() => this.props.screenProps.navigation.navigate('screenName')}.
标签栏
const TabView = createBottomTabNavigator({
Home: {
screen: Home,
},
Contacts: {
screen: Contacts,
},
Group: {
screen: Group,
},
Task: {
screen: Task,
},
Event: {
screen: EventView
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
defaultProps: navigation,
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
let title = ''
if (routeName === 'Home') {
iconName = 'ic_home'
title = "HOME"
} else if (routeName === 'Contacts') {
iconName = 'ic_user_home'
title = "CONTACTS"
} else if (routeName === 'Group') {
iconName = 'ic_group'
title = "PROSPECTS"
} else if (routeName === 'Task') {
iconName = 'ic_document'
title = "TASKS"
} else if (routeName === 'Event') {
iconName = 'ic_calculator'
title = "EVENTS"
}
if (focused) {
return (
<LinearGradient style={{ flex: 1.0, width: '100%' }}
colors={[Constant.COLOR.grediantTop, Constant.COLOR.grediantBottom]}>
<View style={{ flex: 1.0, justifyContent: 'center' }}>
<Image
style={{
height: 25,
width: 25,
tintColor: 'white',
alignSelf: 'center'
}}
tintColor='white'
source={{ uri: iconName }}
resizeMode='contain' />
<SPText
style={{ alignSelf: 'center', marginTop: 2, textAlign: 'center' }}
fontSize={8}
textColor='white'
text={title} />
</View>
</LinearGradient>
)
}
else {
return (
<View style={{ flex: 1.0, justifyContent: 'center' }}>
<Image
style={{
height: 25,
width: 25,
alignSelf: 'center'
}}
source={{ uri: iconName }}
resizeMode='contain' />
<SPText
style={{ alignSelf: 'center', marginTop: 2 }}
fontSize={8}
textColor='gray'
text={title} />
</View>
)
}
},
tabBarOnPress: () => {
const { routeName } = navigation.state;
navigation.navigate(routeName)
},
}),
tabBarOptions: {
showLabel: false,
inactiveTintColor: 'gray',
inactiveBackgroundColor: 'white',
},
})
const RootStack = createStackNavigator(
{
TabView: {
screen: TabView,
},
},
{
mode: 'modal',
headerMode: 'none',
}
);
const Stack = createAppContainer(RootStack);
看来,您对导航的工作原理有误解!在 React Native 应用程序的子组件系统中。
如果组件在创建时作为屏幕包装在导航器对象中,它将可以直接访问导航道具!
但是如果你有一个子组件在一个组件的渲染中并且它不是一个屏幕(当然)那么你必须手动将导航作为道具传递给它!
在我看来这根本不是一个好方法!但在你的情况下,它会起作用
NOTE : IF I AM CORRECT POST YOUR CODE OF SUBCOMPONENTS! AND I WILL
HELP SHOWING THE EXAMPLE
我的 initialRouteName 是 loginScreen。
登录后第二个屏幕出现 BottamTabNavigation。 BottomTab 包含 4 个屏幕,其中一个名为 GroupScreen 的屏幕转到 TopTabScren。我无法导航至此屏幕。
流程是 --> loginScreen --> BottomTabScreen --> TopTabScreen。
我无法导航到 TopTabScreen。它给出了错误
"undefined is not an object(evaluating'this.props.navigation.navigate' ".
TopTabScreen 导致进一步的 4 个屏幕
但是当我设置 InitailRoutName= "TobTabScreen" 时,所有四个屏幕都可以使用它。
对于导航屏幕,我正在使用这个。
onPress={() => this.props.navigation.navigate('screenName')}.
获取底部标签栏屏幕导航道具
_getScreenProps = () => {
return (
{
navigation: this.props.navigation,
}
)
}
渲染标签栏
render() {
return (
<View style={{ flex: 1.0 }}>
<Stack screenProps={this._getScreenProps()} />
</View>
)
}
在 Tab 屏幕上使用导航如下
onPress={() => this.props.screenProps.navigation.navigate('screenName')}.
标签栏
const TabView = createBottomTabNavigator({
Home: {
screen: Home,
},
Contacts: {
screen: Contacts,
},
Group: {
screen: Group,
},
Task: {
screen: Task,
},
Event: {
screen: EventView
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
defaultProps: navigation,
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
let title = ''
if (routeName === 'Home') {
iconName = 'ic_home'
title = "HOME"
} else if (routeName === 'Contacts') {
iconName = 'ic_user_home'
title = "CONTACTS"
} else if (routeName === 'Group') {
iconName = 'ic_group'
title = "PROSPECTS"
} else if (routeName === 'Task') {
iconName = 'ic_document'
title = "TASKS"
} else if (routeName === 'Event') {
iconName = 'ic_calculator'
title = "EVENTS"
}
if (focused) {
return (
<LinearGradient style={{ flex: 1.0, width: '100%' }}
colors={[Constant.COLOR.grediantTop, Constant.COLOR.grediantBottom]}>
<View style={{ flex: 1.0, justifyContent: 'center' }}>
<Image
style={{
height: 25,
width: 25,
tintColor: 'white',
alignSelf: 'center'
}}
tintColor='white'
source={{ uri: iconName }}
resizeMode='contain' />
<SPText
style={{ alignSelf: 'center', marginTop: 2, textAlign: 'center' }}
fontSize={8}
textColor='white'
text={title} />
</View>
</LinearGradient>
)
}
else {
return (
<View style={{ flex: 1.0, justifyContent: 'center' }}>
<Image
style={{
height: 25,
width: 25,
alignSelf: 'center'
}}
source={{ uri: iconName }}
resizeMode='contain' />
<SPText
style={{ alignSelf: 'center', marginTop: 2 }}
fontSize={8}
textColor='gray'
text={title} />
</View>
)
}
},
tabBarOnPress: () => {
const { routeName } = navigation.state;
navigation.navigate(routeName)
},
}),
tabBarOptions: {
showLabel: false,
inactiveTintColor: 'gray',
inactiveBackgroundColor: 'white',
},
})
const RootStack = createStackNavigator(
{
TabView: {
screen: TabView,
},
},
{
mode: 'modal',
headerMode: 'none',
}
);
const Stack = createAppContainer(RootStack);
看来,您对导航的工作原理有误解!在 React Native 应用程序的子组件系统中。
如果组件在创建时作为屏幕包装在导航器对象中,它将可以直接访问导航道具!
但是如果你有一个子组件在一个组件的渲染中并且它不是一个屏幕(当然)那么你必须手动将导航作为道具传递给它!
在我看来这根本不是一个好方法!但在你的情况下,它会起作用
NOTE : IF I AM CORRECT POST YOUR CODE OF SUBCOMPONENTS! AND I WILL HELP SHOWING THE EXAMPLE