首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取TypeError FlatList响应导航5

获取TypeError FlatList响应导航5
EN

Stack Overflow用户
提问于 2021-02-02 06:03:00
回答 1查看 81关注 0票数 0

当用户单击平面列表中的项时,我想导航到另一个屏幕,但始终得到相同的错误。

TypeError: undefined is not an object (evaluating 'navigation.navigate')

我在snack.expo上创建了一个项目,尝试在屏幕之间移动,它可以工作。这是链接https://snack.expo.io/ad5yaIPIo

但是每当我试图在我的项目上实现时,我都会得到同样的错误。需要帮助。

代码语言:javascript
复制
AuditList.js // Child Component

const AuditList = ({navigation}) => {
  const [selectedId, setSelectedId] = useState(null);
  const ItemRender = ({item}) => {
    const backgroundColor =
      item.id === selectedId ? 'rgba(0, 0, 0, 0.2)' : 'rgba(255, 255, 255, 1)';
    return (
      <AuditItem
        item={item}
        onPress={() => {
          setSelectedId(item.id);
          navigation.navigate('Audit Details'); // Here's the navigation to go to another screens
        }}
        style={{backgroundColor}}
      />
    );
  };
  return (
    <AuditContainer>
      <FlatList
        data={DataList}
        renderItem={ItemRender}
        keyExtractor={(item) => item.id}
        extraData={selectedId}
      />
    </AuditContainer>
  );
};
代码语言:javascript
复制
SiteAudit.js // Parent Component

const InProgress = () => {
  return <AuditList />;
};

const Completed = () => {
  return <AuditList />;
};

const SiteAudit = () => {
  return (
    <Tab.Screen name="In Progress" component={InProgress} />
    <Tab.Screen name="Completed" component={Completed} />
  )
}
代码语言:javascript
复制
StackNavigation.js // Navigate To Another Screens

const StackNavigation = ({navigation}) => {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Site Audit">
        <Stack.Screen name="Site Audit" component={SiteAudit} />
        <Stack.Screen
          name="Audit Details"
          component={AuditDetails}
          options={{
            headerLeft: () => (
              <LeftNavigationButton navigationProps={navigation} />
            ),
            headerTitle: (props) => <HeaderLogo {...props} />,
            headerStyle: {
              height: 150,
              backgroundColor: '#212529',
              borderBottomLeftRadius: 10,
              borderBottomRightRadius: 10,
              shadowColor: '#212529',
              shadowOffset: {
                width: 0,
                height: 2,
              },
              shadowOpacity: 0.7,
              shadowRadius: 3.84,
              elevation: 5,
              alignSelf: 'center',
            },
          }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

编辑

代码语言:javascript
复制
Routes.js // Main Navigation

const Routes = () => {
  return (
    <NavigationContainer theme={RootTheme}>
      {user ? (
        <>
          <DrawerNavigation />
        </>
      ) : (
        <>
          <AuthStack />
        </>
      )}
    </NavigationContainer>
  )
}

我如何在我的主导航中调用AuditDetails?让我们假设我的StackNavigation.jsAuditDetails的导航

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-02 06:37:12

由于您的AuditList不是由您的导航系统直接将作为屏幕呈现为 ((InProgress组件做)). navigation对象不是自动注入的。进入组件道具

所以你要么

  • 使用useNavigation钩子。

从‘@react- useNavigation /本机’导入{useNavigation};const useNavigation=

  • navigation对象传递给您的AuditList组件.

编辑

代码语言:javascript
复制
const InProgress = ({ navigation }) => {
  return <AuditList navigation={navigation} />;
};

编辑2在你的名字里有一个空格.删除它,因为这是用于导航的routeName

代码语言:javascript
复制
 <Stack.Screen
          name="Audit Details" // <-- here

同样的问题也存在于其他航线上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66004396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档