javascript - 无法读取未定义的 react 导航的属性 'routeName'

标签 javascript react-native react-navigation

我正在尝试为我的抽屉布局使用自定义组件。下面是我的自定义抽屉组件:

CustomDrawer.js:

class CustomDrawer extends Component {

  navigateToScreen = (route) => () => {
    const navigateAction = NavigationActions.navigate({
      routeName: route
    });
    this.props.navigation.dispatch(navigateAction);
  }

  render () {
    return (
      <View style={styles.container}>
        <ScrollView>
          <View>
            <Text style={styles.sectionHeadingStyle}>
              Section 1
            </Text>
            <View style={styles.navSectionStyle}>
              <Text style={styles.navItemStyle} onPress={this.navigateToScreen('Page1')}>
              Page1
              </Text>
            </View>
          </View>
          <View>
            <Text style={styles.sectionHeadingStyle}>
              Section 2
            </Text>
            <View style={styles.navSectionStyle}>
              <Text style={styles.navItemStyle} onPress={this.navigateToScreen('Page2')}>
                Page2
              </Text>
              <Text style={styles.navItemStyle} onPress={this.navigateToScreen('Page3')}>
                Page3
              </Text>
            </View>
          </View>
        </ScrollView>
        <View style={styles.footerContainer}>
          <Text>This is my fixed footer</Text>
        </View>
      </View>
    );
  }
}

CustomDrawer.propTypes = {
  navigation: PropTypes.object
};

export default CustomDrawer;

const styles = StyleSheet.create({
  container: {
     paddingTop: 20,
     flex: 1
   },
   navItemStyle: {
     padding: 10
   },
   navSectionStyle: {
     backgroundColor: 'lightgrey'
   },
   sectionHeadingStyle: {
     paddingVertical: 10,
     paddingHorizontal: 5
   },
   footerContainer: {
     padding: 20,
     backgroundColor: 'lightgrey'
   }
});

下面是我的router.js:

const mapNavigationStateParamsToProps = (SomeComponent) => {
  return class extends Component {
        static navigationOptions = SomeComponent.navigationOptions; // better use hoist-non-react-statics
        render() {
            const {navigation: {state: {params}}} = this.props
            return <SomeComponent {...params} {...this.props} />
        }
    }
}

export const MainScreenNavigator = TabNavigator({
 Home: {
   screen: Home,
  navigationOptions : {
     tabBarLabel: 'Home',
     tabBarIcon: ({ tintColor }) => <Icon name="account-circle" size={35} color={tintColor} />
   },
 },
 MyCards: {
   screen: MyCards,
   navigationOptions : {
    tabBarLabel: 'My Cards',
    tabBarIcon: ({ tintColor }) => <Icon name="list" size={35} color={tintColor} />
  },
 },

},
 {
   tabBarPosition: 'bottom',
   animationEnabled: true,
   tabBarOptions: {
     activeTintColor: '#e91e63',
   },
 },
);

export const drawerNavigation = DrawerNavigator({
    Home: {
      screen: Home,
    },
    MyCards: {
      screen: MyCards,
    },
    Profile: {
      screen: Profile,
    },
    SearchUsers: {
      screen: SearchUsers
    },
    CardRequests: {
      screen: CardRequests
    },
    GetCard: {
      screen: GetCard
    }
  }, {
    contentComponent: CustomDrawer,
    drawerWidth: 300
  }

);

drawerNavigation.navigationOptions = {
    header: null,
};

export const AppNavigation = StackNavigator({
    LoginScreen: { screen: Login},
    SignUpScreen: { screen: SignUp },
    Tabs: { screen: drawerNavigation},
    AddCard: { screen: AddCard },
    GetCard: {screen: GetCard},
    SearchedUserProfile: {screen: mapNavigationStateParamsToProps(SearchedUserProfile) }
  },
  {
   headerMode: 'screen'
 });

当我运行该应用程序时,出现以下错误:

Cannot read property 'routeName' of undefined

我在 CustomDrawer 中使用 routerName。谁能告诉我我做错了什么?

最佳答案

我通过添加以下内容解决了这个问题:

 drawerOpenRoute: 'DrawerOpen',
 drawerCloseRoute: 'DrawerClose',
 drawerToggleRoute: 'DrawerToggle',

完整的抽屉导航器:

export const drawerNavigation = DrawerNavigator({
    Home: {
      screen: Home,
    },
    MyCards: {
      screen: MyCards,
    },
    Profile: {
      screen: Profile,
    },
    SearchUsers: {
      screen: SearchUsers
    },
    CardRequests: {
      screen: CardRequests
    },
    GetCard: {
      screen: GetCard
    }
  },{
      contentComponent: SideMenu,
      drawerWidth: 300,
      drawerOpenRoute: 'DrawerOpen',
      drawerCloseRoute: 'DrawerClose',
      drawerToggleRoute: 'DrawerToggle',
  });

希望对你有帮助

关于javascript - 无法读取未定义的 react 导航的属性 'routeName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48483438/

相关文章:

javascript - 使用jquery隐藏/显示图像上的div?

html - React Native : 'top' property behaving as expected, 但 'bottom' 不是

javascript - 在同一 Fetch POST API 调用中返回 JSON

react-native - 如何将调度函数传递给 Header 组件?

javascript - 在屏幕之间传递数据、route.params、React Native 导航

react-navigation - 如何用 NavigationContainer 替换 createAppContainer? (使用兼容层模块)

javascript - 使用push方法创建数组

javascript - 用 JavaScript 编写,无法正确应用函数?

javascript - 使用 javascript/jquery 读取输入字段的值并自动分配 var 类型

reactjs - React - 如何从组件中分离逻辑