react-navigation部分属性介绍
React-Navigation
- npm or yarn install React-navigation
1 | npm install react-navigation --save or |
- 使用 react-navigation 必须创建导航器,react-navigation 自带三种导航器
- StackNavigator - 为您的应用程序在每个新屏幕放置在堆栈之上的屏幕之间进行切换
- TabNavigator -用于设置多个选项卡的屏幕(tab)
- DrawerNavigator -用于设置带抽屉导航的屏幕
页面使用时创建一个 StackNavigator ,最常见的底部切换
StackNavigatorConfig 说明
路由器的选项
- initialRouteName:设置堆栈的默认屏幕。必须匹配路由配置中的一个键。
- initialRouteParams:初始路线的参数
- navigationOptions:用于屏幕的默认导航选项
- paths:覆盖路由配置中设置的路径的映射
视觉选项
- mode:定义渲染和转换的风格:
- card:使用标准的 iOS 和 Android 屏幕转换。这是默认的。
- modal:使屏幕从底部滑入,这是一种常见的 iOS 模式。只适用于 iOS,对 Android 没有任何影响。
- headerMode:指定如何呈现标题:
- float:在屏幕更改时渲染保留在顶部的单个标题和动画。这是 iOS 上的常见模式。
- screen:每个屏幕都有一个标题,标题与屏幕一起淡入淡出。这是 Android 上的常见模式。
- none:没有标题将被渲染。
- cardStyle:使用这个道具覆盖或扩展堆栈中单个卡的默认样式。
- transitionConfig:返回与默认屏幕转换合并的对象的函数(查看类型定义中的 TransitionConfig )。提供的函数将传递以下参数:
- transitionProps:新屏幕的过渡道具。
- prevTransitionProps:过渡屏幕的道具。
- isModal:指定屏幕是模态的布尔值。
- onTransitionStart:卡转换动画即将开始时调用的函数。
- onTransitionEnd:卡转换动画完成后调用的函数。
Screen navigationOptions 屏幕导航参数说明
- title:可以用作回退的字符串。此外,将用作(如果嵌套在 TabNavigator 中)或(如果嵌套在 DrawerNavigator 中)
- headerTitle
- tabBarLabel
- drawerLabel
- header:React 元素或给定的函数返回 React 元素,显示为标题。设置为隐藏标题。Header:null
- headerTitle:字符串,React 元素或 React 组件使用的标题。默认为上一级组件的名称。
- headerTitleAllowFontScaling:标题标题字体是否应缩放以尊重文本大小可访问性设置。默认为 true。
- headerBackTitle:iOS 上后退按钮使用的标题字符串,或禁用标签。默认为前一个场景的名称。隐藏设置为 null
- headerTruncatedBackTitle:后退按钮使用的标题字符串不适合屏幕时。默认。headerBackTitle 是”Back”
- headerRight:React 元素显示在标题的右侧。
- headerLeft:React 元素或组件显示在标题的左侧。当使用的成分。
- headerStyle:标题的样式 obj
- headerTitleStyle:标题组件的样式 obj
- headerBackTitleStyle:组件返回字体标题的样式 obj
- headerTintColor:头部的颜色
- headerPressColorAndroid:材质纹波的颜色(仅适用于 Android> = 5.0)
- gesturesEnabled:是否可以使用手势来消除此屏幕。在 iOS 上默认为 true,在 Android 上为 false。
- gestureResponseDistance:对象覆盖从屏幕边缘开始触摸的距离,以识别手势。它具有以下属性:
- horizontal- 数字 - 水平方向的距离。默认为 25。
- vertical- 数字 - 垂直方向的距离。默认为 135。
- gestureDirection:string 来覆盖关闭手势的方向。正常行为或从右向左滑动。default、inverted
页面使用 TabNavigator 标签栏
1 | /**TabNavigator */ |
页面使用 DrawerNavigator 抽屉
1 | const drawerRouteConfigs = { |
navigation 解释
在 StackNavigator 中注册后的组件都有 navigation 这个属性.navigation 又有 5 个参数:navigate,goBack,state,setParams,dispatch,可以在组件下 console.log 一下 this.props 就能看到
this.props.navigation.navigate(‘routeName’, {params:params}, action): push 下一个页面
- routeName: 注册过的目标路由名称 string
- params: 传递的参数 props
- action: 集成 redux dispatch 一个 action
this.props.navigation.goBack(): 返回上一页
this.props.navigation.state:每个界面通过这去访问它的 router,state 其中包括了:
- routeName: 路由名
- key: 路由身份标识
- params: props 参数
this.props.navigation.setParams: 该方法允许界面更改 router 中的参数,可以用来动态的更改导航栏的内容
this.props.navigation.dispatch: 可以 dispatch 一些 action,主要支持的 action 有:
- Navigate:
1
2
3
4
5
6
7
8
9
10
11import { NavigationActions } from "react-navigation";
const navigationAction = NavigationActions.navigate({
routeName: "Profile",
params: {},
// navigate can have a nested navigate action that will be run inside the child router
action: NavigationActions.navigate({ routeName: "SubProfileRoute" }),
});
this.props.navigation.dispatch(navigationAction);- SetParams:为指定的 router 更新参数,该参数必须是已经存在于 router 的 param 中
1
2
3
4
5
6
7
8import { NavigationActions } from "react-navigation";
const setParamsAction = NavigationActions.setParams({
params: {}, // these are the new params that will be merged into the existing route params
// The key of the route that should get the new params
key: "screen-123",
});
this.props.navigation.dispatch(setParamsAction);- Reset:Reset 方法会清除原来的路由记录,添加上新设置的路由信息, 可以指定多个 action,index 是指定默认显示的那个路由页面
1
2
3
4
5
6
7
8
9
10import { NavigationActions } from "react-navigation";
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: "Profile" }),
NavigationActions.navigate({ routeName: "Two" }),
],
});
this.props.navigation.dispatch(resetAction);