You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Native Base自定义TabNavigator时出现props.navigation.navigate不是函数错误

解决Native Base自定义TabNavigator时的props.navigation.navigate is not a function错误

我帮你排查了代码里的问题,主要有两个关键点导致这个错误:

1. 路由名称匹配错误

你在自定义底部Tab的第二个Button里写了:

onPress={() => props.navigation.navigate('CameraRouter')}

但你在createBottomTabNavigator里定义的路由名称是Camera(对应的screen组件才是CameraRouter),路由跳转必须使用你定义的路由名称,而不是组件名。所以这里应该改成:

onPress={() => props.navigation.navigate('Camera')}

2. 确保正确获取navigation对象

虽然react-navigation会自动给tabBarComponent注入navigation props,但通过解构props的方式可以更清晰地确保获取正确,避免潜在的嵌套props问题:

tabBarComponent: ({ navigation }) => {
  return (
    <Footer>
      <FooterTab>
        <Button vertical onPress={() => navigation.navigate('Feed')}>
          <Icon name="bowtie" />
          <Text>Lucy</Text>
        </Button>
        <Button vertical onPress={() => navigation.navigate('Camera')}>
          <Icon name="briefcase" />
          <Text>Nine</Text>
        </Button>
        <Button vertical>
          <Icon name="headset" />
          <Text>Jade</Text>
        </Button>
      </FooterTab>
    </Footer>
  );
}

修改后的完整MainRouter代码

import { createBottomTabNavigator } from 'react-navigation'; 
import React from 'react'; 
import { Button, Text, Icon, Footer, FooterTab } from 'native-base'; 
import CameraRouter from './CameraRouter'; 
import Feed from '../components/Feed'; 

const MainRouter = createBottomTabNavigator(
  { 
    Feed: { 
      screen: Feed, 
    }, 
    Camera: { 
      screen: CameraRouter, 
      navigationOptions: { 
        tabBarVisible: false, 
      }, 
    }, 
  }, 
  { 
    tabBarComponent: ({ navigation }) => { 
      return ( 
        <Footer> 
          <FooterTab> 
            <Button vertical onPress={() => navigation.navigate('Feed')}> 
              <Icon name="bowtie" /> 
              <Text>Lucy</Text> 
            </Button> 
            <Button vertical onPress={() => navigation.navigate('Camera')}> 
              <Icon name="briefcase" /> 
              <Text>Nine</Text> 
            </Button> 
            <Button vertical> 
              <Icon name="headset" /> 
              <Text>Jade</Text> 
            </Button> 
          </FooterTab> 
        </Footer> 
      ); 
    } 
  } 
); 

export default MainRouter;

这样修改后,应该就能解决props.navigation.navigate is not a function的错误了,核心就是确保跳转的路由名称和你在createBottomTabNavigator里定义的一致,同时正确获取navigation对象。

内容的提问来源于stack exchange,提问作者Alexander Valtingojer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:53:54