reactjs - 子屏幕中的 React-Native 导航?
问题描述
如何在子屏幕中使用导航?
我在 App.js 中创建导航。然后为每个屏幕创建堆栈导航器(在本例中为 FindGroupScreen)。在 FindGroupScreen.js 中,我创建了一个子屏幕 (TravelListDetail),我想在其中使用导航。在 FindGroupScreen 中时,我通常只使用
this.props.navigation.navigate('Chat');
导航到另一个屏幕。但这在子屏幕(TravelListDetail)中不起作用。我应该怎么做才能使导航在子屏幕中工作?
应用程序.js:
imports ...
const FindGroupStack = createStackNavigator({
FindGroup: FindGroupScreen,
},
{ headerMode: 'none', }
);
// ...stacks
const MainBottomTab = createBottomTabNavigator(
{
Home: HomeStack,
FindGroup: FindGroupStack,
Trip: TripStack,
Chat: ChatStack,
Menu: MenuStack,
},
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === 'Home') {
//iconName = `facebook${focused ? '' : '-outline'}`;
iconName = `home`;
} else if (routeName === 'FindGroup') {
iconName = `map-marked-alt`;
} else if (routeName === 'Trip') {
iconName = `map-marker-alt`;
} else if (routeName === 'Chat') {
iconName = `comments`;
} else if (routeName === 'Menu') {
iconName = `bars`;
}
//return <Ionicons name={iconName} size={25} color={tintColor} />;
return <Icon name={iconName} size={20} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: '#f0ca6d',
inactiveTintColor: '#ffffff',
labelStyle: {
fontSize: 12,
},
style: {
backgroundColor: '#303030',
},
},
}
);
export default createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: MainBottomTab,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
查找组屏幕:
imports ...
import TravelListDetail from '../Detail/TravelListDetail';
class FindGroupScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedItem: null,
phase: 'phase-0',
};
}
renderPage() {
const { selectedItem, position, detailItem, phase } = this.state;
return (
<View style={{ flex: 1, backgroundColor: '#606060' }}>
<List
selectedItem={selectedItem}
onItemPress={this.onItemPressed}
phase={phase}
/>
<TravelListDetail
phase={phase}
selectedItem={selectedItem}
onBackPress={this.onBackPressed}
onSharedElementMovedToDestination={
this.onSharedElementMovedToDestination
}
onSharedElementMovedToSource={this.onSharedElementMovedToSource}
/>
</View>
);
}
render() {
const {
phase,
} = this.state;
return (
<SharedElementRenderer>
<View style={styles.container}>
<ToolbarBackground
isHidden={phase !== 'phase-1' && phase !== 'phase-2'}
/>
{this.renderPage()}
</View>
</SharedElementRenderer>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default FindGroupScreen;
旅游清单详情:
<View style={styles2.viewCenter}>
<TouchableOpacity
style={styles2.buttonStyle}
activeOpacity = { .5 }
onPress={ this.gotoChatScreen }
>
<Text style={styles2.buttonTextStyle}> Share Travel </Text>
</TouchableOpacity>
</View>
解决方案
将其作为道具传递给子屏幕。
<TravelListDetail
navigation={this.props.navigation}
phase={phase}
selectedItem={selectedItem}
onBackPress={this.onBackPressed}
onSharedElementMovedToDestination={
this.onSharedElementMovedToDestination
}
onSharedElementMovedToSource={this.onSharedElementMovedToSource}
/>
然后,在孩子身上,你写:
props.navigation.navigate('Welcome')
this.
(如果它是类组件,则带有前缀)。
推荐阅读
- javascript - php文件内的内联javascript中的Vscode实验性装饰器警告
- python - Netmiko SecureCopy 进度条
- c# - 画布 C# 中的 dxf 文件
- sql - spark中变量生成的问题
- c# - 创建一对多和多对一关系 - SQLite & Entity Framework
- php - PHP中的PDO删除查询不生效
- php - 无法验证来自数据库 symfony 4 的用户重定向到相同的登录页面
- algorithm - Big O 符号的数学逼近与编程中的实用数学逼近有什么关系
- c# - unity FollowMousePos
- linux - 如何创建一个 bash 文件来显示两个或多个带有命令的屏幕区域?