简体   繁体   English

嗨,我正在使用 react-native 和 react-navigation

[英]hi i'm using react-native and react-navigation

when i try to navigation.navigate this error occure当我尝试导航时。导航发生此错误

Cannot read property 'navigate' of undefined无法读取未定义的属性“导航”

this is my code这是我的代码

when i try to navigation.navigate this error occure当我尝试导航时。导航发生此错误

Cannot read property 'navigate' of undefined无法读取未定义的属性“导航”

this is my code这是我的代码

card.js卡片.js

const Card = ({
  navigation,
  item,
  ingredients1,
  ingredients2,
  ingredients3,
}) => {
  console.log("ingredients1:", ingredients1);
  console.log("ingredients2:", ingredients2);
  console.log("ingredients3:", ingredients3);

  const ingredients = ingredients1 + "," + ingredients2 + "," + ingredients3;
  const ingre1 = ingredients.split(",");
  const ingre = ingre1.filter(function (tem) {
    return tem !== null && tem !== undefined && tem !== "";
  });
  const continents = item.content1 + "," + item.content2 + "," + item.content3;
  let score = 0;
  for (let i = 0; i < ingre.length; i++) {
    if (continents.indexOf(ingre[i]) != -1) {
      score += 33;
    }
  }
  const _score = String(score);
  const score1 = _score.split(",");

  console.log("score1:", score1);

  const RecipeButton = useCallback(() => {
    navigation.navigate("Explain");
  }, []);

  return (
    <>
      <ImageStyle
        source={{ uri: `http://10.0.2.2:3065/${item.Images[0].src}` }}
      />
      <Explain>
        <Username>{item.User.nickname}</Username>
        <FoodName>{item.name}</FoodName>

        <Ingrepercent>재료 {score1}%일치</Ingrepercent>

        <Recipe onPress={RecipeButton}>
          <Label>Recipe</Label>
        </Recipe>
      </Explain>
    </>
  );
};

export default Card;

navigator.js导航器.js

const MainNavigator = () => {
  return (
    <Stack.Navigator>
      <Stack.Screen
        name="Main"
        component={Main}
        options={{
          headerShown: false,
        }}
      />
      <Stack.Screen
        name="Food"
        component={Food}
        options={{
          headerShown: false,
        }}
      />
      <Stack.Screen
        name="Explain"
        component={Explain}
        options={{
          headerShown: false,
        }}
      />
      <Stack.Screen
        name="Card"
        component={Card}
        options={{
          headerShown: false,
        }}
      />
    </Stack.Navigator>
  );
};

i don't know how can i resolve....我不知道我该如何解决......

please help me请帮我

this is error view这是错误视图

在此处输入图像描述

This Worked for me!这对我有用!

import { useNavigation } from '@react-navigation/native';
function funcName() {
  const navigation = useNavigation();

  return (
   {/* Your code /*}
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-Native —如何使用react-navigation传递参数? - React-Native — How to pass parameters using react-navigation? 如何在 react-native 和 react-navigation 上使用反应钩子 - How to use react hooks on react-native with react-navigation 我正在使用 react-navigation/native 版本 6^ 但我的 react-navigation 是版本 4 - I am using react-navigation/native version 6^ but my react-navigation is version 4 使用react-navigation在React-Native中的DrawerNavigator中出现问题 - Problems in DrawerNavigator in React-Native with react-navigation 具有react-navigation的React-Native Handle推送通知 - React-Native Handle push notification with react-navigation 如何在 React-navigation/react-native 中隐藏后退按钮 - how to hide back button in React-navigation/react-native 在反应本地/反应导航应用程序中存储全局状态? - Storing global state in react-native/react-navigation app? 反应本机启动屏幕和反应导航 - React-native splash screen and react-navigation React-Native:组件仅在使用react-native-navigation或react-navigation时在加载的最后一个选项卡上正常工作 - React-Native: components only working properly on the last tab loaded in when using react-native-navigation or react-navigation 在react-native中使用react-navigation时的空白视图 - Blank view when i use react-navigation in react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM