RN里面的导航是依赖三方库的,作的不错,官方推荐这个react-navigationreact
导入方式,在.package里面这样加入三方库名称面试
或者在根目录下输入 npm install --save react-navigation 便可npm
导入结果以下react-native
"dependencies": {
"@ant-design/react-native": "^3.1.5",
"react-native-gesture-handler": "^1.3.0",
"react-native-image-crop-picker": "^0.24.1",
"react": "16.8.6",
"react-native": "0.60.5",
"react-navigation": "^3.11.0"
},
复制代码
下面介绍下tabbar和navigationbash
const Tabs = createBottomTabNavigator({
HomePage: {
screen: Home,
navigationOptions: {
tabBarLabel: '首页',
tabBarIcon: ({focused, tintColor}) => (
<view/>
)
}
},
MinePage: {
screen: Mine,
navigationOptions: {
tabBarLabel: '个人',
tabBarIcon: ({focused, tintColor}) => (
<view/>
)
}
},
}, {
tabBarOptions: {
style: {
height: 49,
backgroundColor: '#fff',
paddingBottom: 22,
paddingTop: 6,
// paddingBottom: 4,
borderTopWidth: 0,
shadowOffset: {width: 0, height: 2},
shadowOpacity: 0.2,
shadowRadius: 4,
shadowColor: 'black',
elevation: 4,
},
inactiveTintColor: 'black',
activeTintColor: 'blue'
}
tabBarComponent: props => <CustomTabbar {...props} /> //自定义相关
});
复制代码
上面的能够理解为定义了一个tabbar控制器,Tab即为其控制器名称app
页面栈(navigation),其形式为一个栈的形式,app启动时默认加载第一个标签,ui
let RootStack = createStackNavigator({
TabsPage: {
screen: HomePage,
navigationOptions: {
header: null
}
},
MyInfo: {
screen: MyInfo
}
}, {
defaultNavigationOptions: ({navigation}) => {
const {
state: {
routeName
}
} = navigation
return {
gesturesEnabled: true,
headerBackTitle: null,
headerStyle: {
backgroundColor: '#172435',
borderBottomWidth: 0,
elevation: 0
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff',
fontSize: 24
},
headerLeft: (
<TouchableOpacity
style={[
{
width: 60,
alignItems: 'center',
justifyContent: 'center'
},
]}
onPress={() => {
const {
index
} = navigation.dangerouslyGetParent().state
if (index !== 0) {
navigation.pop()
} else {
}
}}
>
</TouchableOpacity>
)
}
},
transitionConfig: () => ({
screenInterpolator: CardStackStyleInterpolator.forHorizontal,
})
});
复制代码
对外声明页面栈,用于启动appthis
const App = createAppContainer(RootStack);
复制代码
这样设置完毕便可尝试使用app了,设置完以前本身建立几个页面试试吧spa
按照上面设置完成页面栈以后code
跳转push:(第一个参数为声明的页面名称, 第二个为传递的参数,为一个对象)
this.props.navigation.push('MyInfo', {item: myInfo})
复制代码
注:若想回调传递一个callback回调便可
this.props.navigation.push('MyInfo', {
callback: ()=>{
//我是回调
}
})
复制代码
接收对象:(跳转后的页面,param为传递过来的参数)
let param = this.props.navigation.state.params
复制代码
返回pop:
this.props.navigation.goback()
复制代码
切换栈navigate:(用于切换栈,若是当前栈里面没有改页面会push进入一个页面,若是有页面会切换到前面指定的页面,上面的页面会被pop掉,也能够经过这个切换tabbar标签)
this.props.navigation.navigate('MyInfo')
复制代码
导航相关的就想介绍到这些了,详细的属性能够去react-navigation官网查看吆,更有助与理解