使用动态选项卡创建 TabNavigator
Creating a TabNavigator with dynamic tabs
我相对确定我发现这是不可能的,但我想确保没有办法。
有问题的应用程序以 AppNavigator StackNavigator
开始。
export const AppNavigator = StackNavigator({
Login: {
screen: Login,
navigationOptions: ({navigation}) => ({
title: 'Aanmelden',
params: {
nuke: navigation.state.params && !!navigation.state.params.nuke,
},
}),
},
Main: {
screen: DynamicTabBar,
navigationOptions: ({navigation}) => ({
title: 'Follow-up',
}),
},
}, {
mode: 'modal',
headerMode: 'none',
});
export class AppWithNavigationState extends React.Component {
constructor(props) {
super(props);
}
render() {
return <AppNavigator navigation={addNavigationHelpers({ dispatch: this.props.dispatch, state: this.props.navigationReducer })} />
}
}
AppWithNavigationState.propTypes = {
dispatch: React.PropTypes.func.isRequired,
navigationReducer: React.PropTypes.object.isRequired,
};
const mapStateToProps = state => ({
navigationReducer: state.navigationReducer,
});
export default connect(mapStateToProps)(AppWithNavigationState);
到目前为止一切顺利,只是 DynamicTabBar
不应该是 'rendered' 直到用户登录(即从 Login
导航到 Main
) .
原因如下
const Tabs = TabNavigator({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
export default class DynamicTabBar extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Tabs navigation={this.props.navigation} />;
}
}
DynamicTabBar.router = Tabs.router;
...Account.User.CanEnter ? {
始终为 false,因为 TabNavigator
在用户登录之前呈现并且 Account
已填充。一次失败的尝试,它接缝。
我想在 componentWillMount
中填充 Tabs
,但是我无法设置静态路由器:DynamicTabBar.router = Tabs.router;
关于如何解决这个问题有什么想法吗?
是的!
我想我找到了办法。解决方案可能不遵循设计模式,但这是我想出的:
export default class DynamicTabBar extends React.Component {
static router = TabRouter({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
constructor(props) {
super(props);
}
render() {
const tabs = TabNavigator({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
return <Tabs navigation={this.props.navigation} />;
}
}
路由器以静态方式分配,并在运行时动态构建。
这是我的解决方案。我正在使用世博会。我需要标签的动态数量和内容,我将从 API 中获取。
MainTabNavigator.js
import React from 'react';
import { Platform } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
import TabBarIcon from '../components/TabBarIcon';
import SettingsScreen from '../screens/SettingsScreen';
import {getBrowerScreen} from '../screens/BrowserScreen';
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
});
SettingsStack.navigationOptions = {
tabBarLabel: 'Settings',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-options' : 'md-options'}
/>
),
};
// Tab params that we will get from API
var tabParams = [
{title: 'Tab 1', url: 'https://mycar.city', icon: 'md-pricetags'},
{title: 'Tab 2', url: 'https://mycar.city', icon: 'ios-play-circle'},
{title: 'Tab 3', url: 'https://mycar.city', icon: 'ios-phone-portrait'},
{title: 'Tab 4', url: 'https://global-trend.info', icon: 'ios-phone-portrait'},
],
tabs = {};
for (var i=0; i<tabParams.length; i++) {
const tab = tabParams[i];
tabs['Tab' + (i + 1)] = createStackNavigator({
Links: getBrowerScreen(tab.url),
});
tabs['Tab' + (i + 1)].navigationOptions = {
tabBarLabel: tab.title,
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={tab.icon}
/>
),
};
}
//Adding another kind of tab
tabs.SettingsStack = SettingsStack;
export default createBottomTabNavigator(tabs);
BrowserScreen.js
import React from 'react';
import { View, StyleSheet, WebView } from 'react-native';
import { ExpoLinksView } from '@expo/samples';
export function getBrowerScreen(url) {
return class BrowserScreen extends React.Component {
static navigationOptions = {
header: null,
};
render() {
return (
<View style={styles.container}>
<WebView
source={{ uri: url }}
style={{ width: '100%' }}
/>
</View>
);
}
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});
我知道这已经过时了,但是 AFAICT 在 2019 年仍然没有这样做的好方法。这是我想出的解决方案,我认为它相当简单。
基本上,我使用自定义 getButtonComponent
方法指定自定义 tabBarComponent
,根据当前用户呈现或不呈现按钮。如果用户不应该看到导航项,那么它会呈现一个空视图,具有隐藏导航项的效果。
import { createBottomTabNavigator } from 'react-navigation'
import { BottomTabBar } from 'react-navigation-tabs'
class TabBar extends React.Component {
render() {
return (
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
/>
)
}
getButtonComponent({ route }) {
if (route.key === 'Other') {
return () => <View /> // a view that doesn't render its children
} else {
return null // use the default nav button component
}
}
}
const AppNav = createBottomTabNavigator(
{
Home: HomeStack,
Other: OtherScreen,
},
{
tabBarComponent: TabBar,
}
)
我相对确定我发现这是不可能的,但我想确保没有办法。
有问题的应用程序以 AppNavigator StackNavigator
开始。
export const AppNavigator = StackNavigator({
Login: {
screen: Login,
navigationOptions: ({navigation}) => ({
title: 'Aanmelden',
params: {
nuke: navigation.state.params && !!navigation.state.params.nuke,
},
}),
},
Main: {
screen: DynamicTabBar,
navigationOptions: ({navigation}) => ({
title: 'Follow-up',
}),
},
}, {
mode: 'modal',
headerMode: 'none',
});
export class AppWithNavigationState extends React.Component {
constructor(props) {
super(props);
}
render() {
return <AppNavigator navigation={addNavigationHelpers({ dispatch: this.props.dispatch, state: this.props.navigationReducer })} />
}
}
AppWithNavigationState.propTypes = {
dispatch: React.PropTypes.func.isRequired,
navigationReducer: React.PropTypes.object.isRequired,
};
const mapStateToProps = state => ({
navigationReducer: state.navigationReducer,
});
export default connect(mapStateToProps)(AppWithNavigationState);
到目前为止一切顺利,只是 DynamicTabBar
不应该是 'rendered' 直到用户登录(即从 Login
导航到 Main
) .
原因如下
const Tabs = TabNavigator({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
export default class DynamicTabBar extends React.Component {
constructor(props) {
super(props);
}
render() {
return <Tabs navigation={this.props.navigation} />;
}
}
DynamicTabBar.router = Tabs.router;
...Account.User.CanEnter ? {
始终为 false,因为 TabNavigator
在用户登录之前呈现并且 Account
已填充。一次失败的尝试,它接缝。
我想在 componentWillMount
中填充 Tabs
,但是我无法设置静态路由器:DynamicTabBar.router = Tabs.router;
关于如何解决这个问题有什么想法吗?
是的! 我想我找到了办法。解决方案可能不遵循设计模式,但这是我想出的:
export default class DynamicTabBar extends React.Component {
static router = TabRouter({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
constructor(props) {
super(props);
}
render() {
const tabs = TabNavigator({
Start: {
screen: UserStackNavigator,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="users"
color="#dddddd"
size={20}
/>),
},
},
...Account.User.CanEnter ? {
ConditionalTab: {
screen: ConditionalScreen,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="recycle"
color="#dddddd"
size={20}
/>),
},
}} : {},
Settings: {
screen: Settings,
navigationOptions: {
tabBarIcon: (<Icon
type="font-awesome"
name="cog"
color="#dddddd"
/>),
}
}
},{
...TabNavigator.Presets.AndroidTopTabs,
tabBarPosition: "bottom",
tabBarOptions: {
activeTintColor: '#eaeb65',
showIcon: true,
showLabel: false,
style: { backgroundColor: '#333' },
}
});
return <Tabs navigation={this.props.navigation} />;
}
}
路由器以静态方式分配,并在运行时动态构建。
这是我的解决方案。我正在使用世博会。我需要标签的动态数量和内容,我将从 API 中获取。
MainTabNavigator.js
import React from 'react';
import { Platform } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
import TabBarIcon from '../components/TabBarIcon';
import SettingsScreen from '../screens/SettingsScreen';
import {getBrowerScreen} from '../screens/BrowserScreen';
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
});
SettingsStack.navigationOptions = {
tabBarLabel: 'Settings',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-options' : 'md-options'}
/>
),
};
// Tab params that we will get from API
var tabParams = [
{title: 'Tab 1', url: 'https://mycar.city', icon: 'md-pricetags'},
{title: 'Tab 2', url: 'https://mycar.city', icon: 'ios-play-circle'},
{title: 'Tab 3', url: 'https://mycar.city', icon: 'ios-phone-portrait'},
{title: 'Tab 4', url: 'https://global-trend.info', icon: 'ios-phone-portrait'},
],
tabs = {};
for (var i=0; i<tabParams.length; i++) {
const tab = tabParams[i];
tabs['Tab' + (i + 1)] = createStackNavigator({
Links: getBrowerScreen(tab.url),
});
tabs['Tab' + (i + 1)].navigationOptions = {
tabBarLabel: tab.title,
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={tab.icon}
/>
),
};
}
//Adding another kind of tab
tabs.SettingsStack = SettingsStack;
export default createBottomTabNavigator(tabs);
BrowserScreen.js
import React from 'react';
import { View, StyleSheet, WebView } from 'react-native';
import { ExpoLinksView } from '@expo/samples';
export function getBrowerScreen(url) {
return class BrowserScreen extends React.Component {
static navigationOptions = {
header: null,
};
render() {
return (
<View style={styles.container}>
<WebView
source={{ uri: url }}
style={{ width: '100%' }}
/>
</View>
);
}
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});
我知道这已经过时了,但是 AFAICT 在 2019 年仍然没有这样做的好方法。这是我想出的解决方案,我认为它相当简单。
基本上,我使用自定义 getButtonComponent
方法指定自定义 tabBarComponent
,根据当前用户呈现或不呈现按钮。如果用户不应该看到导航项,那么它会呈现一个空视图,具有隐藏导航项的效果。
import { createBottomTabNavigator } from 'react-navigation'
import { BottomTabBar } from 'react-navigation-tabs'
class TabBar extends React.Component {
render() {
return (
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
/>
)
}
getButtonComponent({ route }) {
if (route.key === 'Other') {
return () => <View /> // a view that doesn't render its children
} else {
return null // use the default nav button component
}
}
}
const AppNav = createBottomTabNavigator(
{
Home: HomeStack,
Other: OtherScreen,
},
{
tabBarComponent: TabBar,
}
)