在react-native开发中,我们往往需要用到路由跳转。
使用react-navigation实现功能。
文档在此。
0. 创建路由栈
import {createStackNavigator, createAppContainer} from "react-navigation";
/** 引入路由页面 */
import ChooseSystem from "./pages/account/chooseSystem";
import TabPage from './pages/tabPage/mainTab';
const navigator = createStackNavigator(
{
Welcome: {
screen: Welcome,
navigationOptions: ({navigation}) => ({
header: null, //隐藏顶部导航栏
})
},
ChooseSystem: {
screen: ChooseSystem,
navigationOptions: ({navigation}) => ({
title: `选择体系`,
})
}
},
{
initialRouteName: "Welcome",
headerMode: 'screen',
mode: 'modal',// 定义了渲染和过渡的风格:
defaultNavigationOptions: {
gesturesEnabled: true,
headerBackImage:
<Image
source={require('./images/common/icon_basis_arrow_left.png')}
style={{width: 15, height: 15}}
/>,
headerStyle: { //标题栏样式
backgroundColor: '#fff',
},
headerTintColor: '#595867', //标题文字和按钮颜色
headerTitleStyle: { //标题文字样式
fontWeight: 'bold',
},
},
transitionConfig: () => ({
transitionSpec: {
duration: 300,
},
screenInterpolator: sceneProps => {
const {layout, position, scene} = sceneProps;
const {index} = scene;
const height = layout.initHeight;
const translateY = position.interpolate({
inputRange: [index - 1, index, index + 1],
outputRange: [height, 0, 0],
});
const opacity = position.interpolate({
inputRange: [index - 1, index - 0.99, index],
outputRange: [0, 1, 1],
});
return {opacity, transform: [{translateY}]};
},
}),
}
)
export default createAppContainer(navigator);
1. push路由
这种方式的路由,可以用返回键返回。
const {navigate} = this.props.navigation;
navigate("WalletList");
2. replace路由
这种方式的路由,相当于将路由栈内部的清空,再将目标路由push进栈。
import {NavigationActions, StackActions} from "react-navigation";
const resetAction = StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({routeName})
]
});
this.props.navigation.dispatch(resetAction);