【问题标题】:can't navigate to another page when using createMaterialTopTabNavigator in react native在 react native 中使用 createMaterialTopTabNavigator 时无法导航到另一个页面
【发布时间】:2021-10-28 16:58:45
【问题描述】:

我正在使用 createMaterialTopTabNavigator 并且我有函数 (aa()) 里面有一个 touchableOpacity btn ,当按下它时无法转到另一个页面我不知道是什么问题,所以任何人都可以帮助我吗? ,我也有路线的路线文件, 谢谢

// 这是 App.js:

 const App=()=>{
  return <AppContainer />;
}

export default App;

// 应用容器

 import { createAppContainer } from "react-navigation";
import React from "react";
import {
    View,
    Text,
    Image,
    StyleSheet,
    Alert,
    TouchableOpacity,
  } from "react-native";
import { createMaterialTopTabNavigator } from "react-navigation-tabs";
import Header from "./Header";
import ListProch from "./ListProch";
import ListeEnCours from "./ListeEnCours";
import ListeTermines from "./ListeTermines";


function aa({ navigation }){
  return(
    <TouchableOpacity 
    onPressIn={()=>navigation.navigate("Buy")}
    style={{width:200,height:200,backgroundColor:"red"}}>

    </TouchableOpacity>

  )
}
const TopBarMenu= createMaterialTopTabNavigator(
  {
    Prochaines: {
      screen: aa
    },
    "En cours": {
      screen: ListeEnCours
    },
    Terminés: {
      screen: ListeTermines
    },
   
  },
  {
    tabBarComponent: Header,
    tabBarOptions: {
      
  
      activeTintColor: "#30A6CA",
      inactiveTintColor: "#333333",
      
      
    },
    initialRouteName: "Prochaines"
  }
  
);

const AppContainer = createAppContainer(TopBarMenu);
export default  (AppContainer)

// Routes.js

import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer } from "react-navigation";
import Login from "../Components/Login";
import ForgetPassword from "../Components/ForgetPassword";
import Register from "../Components/Register";
import Terms from "../Components/Terms";
import Home from "../Components/Home";
import ListProch from "../Components/ListProch";
import TopBar from "../Components/TopBar";
import Encheres from "../Components/Encheres";
import Favoris from "../Components/Favoris";
import Profil from "../Components/Profil";
import UpdateInfo from "../Components/UpdateInfo";
import ChangePassword from "../Components/ChangePassword";
import InfoSecurity from "../Components/InfoSecurity";
import InfoLegales from "../Components/InfoLegales";
import Partenariat from "../Components/Partenariat";
import CommentMarche from "../Components/CommentMarche";
import Autres from "../Components/Autres";
import Notification from "../Components/Notification";
import Buy from "../Components/Buy";
import DetailsProduit from "../Components/DetailsProduit";
import EncheresEndirect from "../Components/EncheresEndirect";
import Header from "../Components/Header";
import AppContainer from "../Components/TopBarMenu";
import HomeScreen from "../Components/TopBarMenu";
import Tabs from "../Components/Tabs";
import Index from "../Components/Index";
import IndexAutres from "../Components/IndexAutres";
import IndexProch from "../Components/IndexProch";
const screens = {

  
  AppContainer: {
    screen: AppContainer,
    navigationOptions: {
      headerShown: null,
    },
  },
  
  Header: {
    screen: Header,
    navigationOptions: {
      headerShown: null,
    },
  },
  Buy: {
    screen: Buy,
    navigationOptions: {
      headerShown: null,
    },
  },
  
};

const RouteStack = createStackNavigator(screens);

const Routes = createAppContainer(RouteStack);

export default Routes;

【问题讨论】:

    标签: javascript reactjs react-native native


    【解决方案1】:
    import { useNavigation } from '@react-navigation/native'
    
    function aa(){
      const navigation = useNavigation();
      return(
        <TouchableOpacity 
        onPressIn={()=>navigation.navigate("Buy")}
        style={{width:200,height:200,backgroundColor:"red"}}>
    
        </TouchableOpacity>
    
      )
    }
    
    

    【讨论】:

    • 感谢您的回答,但是在我像您说的那样编辑它之后,它显示此错误:错误:找不到导航对象。您的组件是否在导航器的屏幕内?又是什么问题!
    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    • 你可以通过linkedIn联系我吗?所以我可以很清楚地解释我的问题这是我的 linik 等着你的来信,谢谢链接简介:linkedin.com/in/sfar-hatem-60b009199
    • 看来您正在使用 v4 的反应导航。您愿意将其更新到最新版本,以便我可以为您提供工作代码吗?
    【解决方案2】:

    您也可以在 app.js 中尝试

     import { NavigationContainer } from '@react-navigation/native';
     const App=()=>{
      return(
         <NavigationContainer independent={true} >
           <AppContainer />
         </NavigationContainer>
      ) 
    }
    
    export default App;
    

    【讨论】:

    • 感谢您的回答,但不起作用..还有其他解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多