reactjs - 用户界面小猫 : can't find variable index (BottomNavigation)

标签 reactjs react-native react-native-ui-kitten

实际上我正在尝试使用UI Kitty中的BottomNavigation但是当我尝试从一个导航选项卡切换到另一个导航选项卡时,应用程序崩溃并出现以下错误:

can't find variable index

这是来自 BottomNavigation 的代码,我在其中使用索引

import React from 'react';
import { BottomNavigation, BottomNavigationTab } from 'react-native-ui-kitten';
import { createAppContainer } from 'react-navigation';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Dashboard from '../navigation/Dashboard';
import Settings from '../navigation/Settings';

export const BottomNavigationShowcase = (props) => {


 const onTabSelect = (selectedIndex) => {
   const { [index] : selectedRoute } = props.navigation.state.routes; // INDEX IS USED HERE
   props.navigation.navigate(selectedRoute.routeName);
 };

 return (
   <BottomNavigation
     selectedIndex={props.navigation.state.index}
     onSelect={onTabSelect}>
     <BottomNavigationTab title='Dashboard' />
     <BottomNavigationTab title='Settings' />
   </BottomNavigation>
  );
}

export const BottomTabNavigator = createBottomTabNavigator({
  Dashboard: Dashboard,
  Settings: Settings,
}, {
  initialRouteName: 'Dashboard',
  tabBarComponent: BottomNavigationShowcase,
});

export default createAppContainer(BottomTabNavigator)

这是 App.JS

import NavigationContainer from './components/BottomNavigation';

const App: () => React$Node = () => {

  return (
    <ApplicationProvider mapping={mapping} theme={lightTheme}>
    <IconRegistry icons={EvaIconsPack} />
    <Header/>
    <NavigationContainer/>
    </ApplicationProvider>
  );
};

最佳答案

const { [index] : selectedRoute } = props.navigation.state.routes; // INDEX IS USED HERE 似乎有问题 这不是读取对象的正确方法。 看来你想从 Prop 中读取路线。为什么不做类似的事情

 const routes = props.navigation.state.routes;
 const selectedRoute = routes[selectedIndex];
 props.navigation.navigate(selectedRoute.routeName);

这应该可以修复应用程序因找不到 index 而崩溃的问题

关于reactjs - 用户界面小猫 : can't find variable index (BottomNavigation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240147/

相关文章:

javascript - 检查是否设置了最小宽度

react-native - 框中出现奇怪的十字,代替了 React-native-paper 的复选框

javascript - React Native + Icons : Warning: Failed prop type: Invalid props.样式键tintColor提供给ForwardRef(Text)

react-native-ui-kitten - 热衷于在 EAS CI 中为 UI Kitten 构建配置 Metro?

reactjs - 无法为 react-admin 在 EDIT 模式下使用 ImageField 上传图像

javascript - 为什么总是呈现星号路线?

javascript - 无法让 Jest 与包含主题的样式化组件一起工作

android - 如何在 ReactContextBaseJavaModule 中获取 ApplicationContext?

javascript - react native : How to styling the TextInput?

react-native - 如何在UI-kitten中使用Select组件的clear()方法?