react-native结合react-navigation之TabNavigator

react-native开发需时肯定有tab的切换,或者页面的转调,当然用RN自身的Navigator 也可以但是也不是那么方便react-navigation 就能满足很多大部分需求,如下图的三种切换方式,下面就说下TabNavigator 和StackNavigator的应用,才踏的一个坑,还是太年轻呀,横刀一撸!!!!

主要的界面 用tab 切换即是TabNavigator, 切换如下图

一共四个页面当然配置就如下咯

 

 
  1. // 两个参数 routeConfigs: NavigationRouteConfigMap, config: TabNavigatorConfig = {}

  2. // 一个route对应的页面和tab图标, 一个切换的样式整个tab栏的样式

  3. //tab

  4. export const AppNavigator = TabNavigator({

  5. Hotshow: {screen: hotshow, navigationOptions: {

  6. tabBarLabel: '热映',

  7. tabBarIcon: ({ tintColor, focused }) => (

  8. <Image resizeMode='contain'

  9. source={require('../icon/icon_hot.png')}

  10. style={[style.footImage, {tintColor: tintColor}]}

  11. />

  12. )

  13. }},

  14. Usshow: {screen: usshow, navigationOptions: {

  15. tabBarLabel: '北美',

  16. tabBarIcon: ({ tintColor, focused }) => (

  17. <Image style={[style.footImage, {tintColor: tintColor}]}

  18. resizeMode='contain'

  19. source={require('../icon/icon_us_normal.png')}

  20. />

  21. )

  22. }},

  23. Soonshow: {screen: soonshow, navigationOptions: {

  24. tabBarLabel: '近期',

  25. tabBarIcon: ({ tintColor, focused }) => (

  26. <Image style={[style.footImage, {tintColor: tintColor}]}

  27. resizeMode='contain'

  28. source={require('../icon/icon_soon_normal.png')}

  29. />

  30. )}

  31. },

  32. Nearcinemas: {screen: nearcinemas, navigationOptions: {

  33. tabBarLabel: '影院',

  34. tabBarIcon: ({ tintColor, focused }) => (

  35. <Image style={[style.footImage, {tintColor: tintColor}]}

  36. resizeMode='contain'

  37. source={require('../icon/icon_near_normal.png')}

  38. />

  39. )},

  40. }

  41. }, {

  42. tabBarPosition: 'bottom',

  43. lazy: true, // 是否懒加载

  44. initialRouteName: 'Hotshow',

  45. tabBarOptions: {

  46. showIcon: true,

  47. pressOpacity: 0.8,

  48. style: {

  49. height: 45,

  50. backgroundColor: '#ffffff',

  51. zIndex: 0,

  52. position: 'relative'

  53. },

  54. labelStyle: {

  55. fontSize: 11,

  56. paddingVertical: 0,

  57. marginTop: -4

  58. },

  59. iconStyle: {

  60. marginTop: -3

  61. },

  62. tabStyle: {

  63. backgroundColor: 'rgb(230,69,51)',

  64. },

  65. }

  66. });


TabNavigatorConfig更多具体的参数如下

 

 

 
  1. /**

  2. * Tab Navigator

  3. */

  4. export interface TabNavigatorConfig {

  5. tabBarComponent?: React.ReactElement<any>,

  6. tabBarPosition?: 'top'|'bottom',

  7. swipeEnabled?: boolean,

  8. animationEnabled?: boolean,

  9. lazy?: boolean,

  10. tabBarOptions?: {

  11. activeTintColor?: string,

  12. activeBackgroundColor?: string,

  13. inactiveTintColor?: string,

  14. inactiveBackgroundColor?: string,

  15. showLabel?: boolean,

  16. style?: ViewStyle,

  17. labelStyle?: TextStyle,

  18.  
  19. // Top

  20. showIcon?: boolean,

  21. upperCaseLabel?: boolean,

  22. pressColor?: string,

  23. pressOpacity?: number,

  24. scrollEnabled?: boolean,

  25. tabStyle?: ViewStyle,

  26. indicatorStyle?: ViewStyle

  27. }

  28. initialRouteName?: string,

  29. order?: string[],

  30. paths?: any // TODO: better def

  31. backBehavior?: 'initialRoute'|'none'

  32. }

如上都配置好了,就需要在屏幕上显示 ,下面代码作为展示 主要的还是创建了AppWithNavigationState 然后export 出他, 在root下调用

 

 

 
  1. class AppWithNavigationState extends Component {

  2.  
  3. render() {

  4. return(

  5. <View style={{flex: 1}}>

  6. {this.props.fetchbool ? <Loading/> :

  7. <AppNavigator navigation={

  8. addNavigationHelpers({dispatch: this.props.navigator,

  9. state: this.props.nav})

  10. }/>

  11. }

  12. </View>

  13. )

  14. }

  15. }

  16.  
  17. function mapStateToProeps(state){

  18. return {

  19. fetchbool: state.fetchload.data,

  20. nav: state.nav

  21. }

  22. };

  23. function macthDispatchToProps(dispatch) {

  24. return bindActionCreators({

  25. navigator: navigator,

  26. initHotshowAction: initHotshow,

  27. fetchLoading: fetchLoading

  28. }, dispatch);

  29. }

  30.  
  31. let style = StyleSheet.create({

  32. footImage: {

  33. width: 25,

  34. height: 25

  35. },

  36. });

  37.  
  38.  
  39. export default connect(mapStateToProeps, macthDispatchToProps)(AppWithNavigationState);

结合了redux , nav就是通过state 传递的,在redux目录下创建了一个navigators/reducer

 

 
  1. import { NavigationActions } from 'react-navigation';

  2. import { AppNavigator } from '../../navigators/AppNavigator';

  3.  
  4. const initialNavState = {

  5. index: 0,

  6. routes: [

  7. {

  8. key: 'Hotshow',

  9. routeName:'Hotshow',

  10. },

  11. {

  12. key: 'Usshow',

  13. routeName:'Usshow',

  14. },

  15. {

  16. key: 'Soonshow',

  17. routeName:'Soonshow',

  18. },

  19. {

  20. key: 'Nearcinemas',

  21. routeName:'Nearcinemas',

  22. },

  23. ],

  24. };

  25.  
  26. export const nav = (state = initialNavState, action) => {

  27. let nextState;

  28. switch (action.type) {

  29. case 'Usshow':

  30. return AppNavigator.router.getStateForAction(

  31. NavigationActions.navigate({ routeName: 'Usshow' }),

  32. state

  33. );

  34. case 'Soonshow':

  35. setate.index= 1

  36. return AppNavigator.router.getStateForAction(

  37. NavigationActions.navigate({ routeName: 'Soonshow' }),

  38. state

  39. );

  40. case 'Nearcinemas':

  41. return AppNavigator.router.getStateForAction(

  42. NavigationActions.navigate({ routeName: 'Nearcinemas' }),

  43. state

  44. );

  45. default:

  46. //return AppNavigator.router.getStateForAction(action, state) || state;

  47. // return AppNavigator.router.getStateForAction(

  48. // state

  49. // );

  50. return AppNavigator.router.getStateForAction(

  51. NavigationActions.navigate({ routeName: 'Hotshow' }),

  52. state

  53. ) || state ;

  54. }

  55. }


 

 

 

做到此处,是个tab的四个页面切换还是可以的,问题就在与当我切换到下一个页面时,就出现了状况,

 

在没有做进入下一个页面前,提前ajax请求,当进入了请求,当然能请求成功,但是请求成功后,刚显示的界面会还在显示等待时,尼玛返回上一个界面

这么说有点拗口,不解其意

额,,,, 清洗脱俗,惊鸿一瞥下就给直接返回A了, console.log(this.props.nav)  看看了 输出一次 nav.index = 0 

然后 1 然后 0 ··········就这么又回到原点了,同时在AppWithNavigationState,解决办法想了一个在navigators/reducer里把nav传递的index固定了

 

 
  1. export const nav = (state = initialNavState, action) => {

  2. let nextState;

  3. switch (action.type) {

  4. case 'Usshow':

  5. setate.index= 1

  6. return AppNavigator.router.getStateForAction(

  7. NavigationActions.navigate({ routeName: 'Usshow' }),

  8. state

  9. );

有次当然可以解决,但是 tab按钮不能点击咯,这真是尴尬的一幕,

 

当然还有个蛋疼的直接用TabNavigator 在AppWithNavigationState中的render 会运行四次,即第一个界面加载时,

console.log 输出变知道 当然这样也有办法解决,react 的生命周期呀,最前面的两个 实例化 和存在期,就在存在期入手,

 

 
  1. shouldComponentUpdate(nextProps, nextState) {

  2. return **** ? false : true ;

  3. }

  4. componentWillUpdate(nextProps, nextState) {

  5. return *** ? false : true;

  6. }

render()就减小了开销

 

问题是 tab还是不能点击啊!!!!!!!!

谜底是这样  StackNavigator 需要这个!!!!

 

 
  1. export const StackNavigator = StackNavigator(

  2. {

  3. Tab:{screen: AppNavigator}, // 就是整个tab切换的 AppNavigator

  4. Product:{screen: DtlWebView}

  5. },

  6. {

  7. stackConfig:{

  8. header: null,

  9. headerBackTitle:null,

  10. headerTintColor:'#333333',

  11. showIcon:true,

  12. swipeEnabled:false,

  13. animationEnabled:false,

  14. initialRouteName: 'Hotshow'

  15. },

  16. mode:'card',

  17. }

  18. );

  19.  

stackConfig 主要参数

 

 TabNavigator, StackNavigator配合应用就很好区分开了前者模块页面之间,后者Stack由字面理解就是通过栈的方式进行切换,最新的就压入到栈顶显示

在App 里之前的就改为StackNavigator

 

 
  1. class App extends Component {

  2.  
  3.  
  4. render() {

  5. return (

  6.  
  7. <Provider store={ store }>

  8. <StackNavigator />

  9. </Provider>

  10. );

  11. }

  12. }

到此就结束咯

 

不对之处,敬请更正··········害羞

 

转自:https://blog.csdn.net/ling369523246/article/details/74783288

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值