javascript - react 导航如何从自定义抽屉导航导航

标签 javascript react-native react-navigation

我正在使用 createDrawerNavigator 并在其中制作了自定义配置文件 View 。当我按下 navigate 时,我想移动到另一个屏幕。

但这给我一个错误undefined is not an object (evaluating"_this.props.navigation")。其他组件 onetwothree 工作正常。

这是我的代码:

export default class App extends Component {
  render() {

    return (
      <View style={{flex:1, marginTop:30}}>
        <AppContainer/>
      </View>
    );
  }
}

    const CustomDrawerContentComponent = (props)=> {
      return(
      <SafeAreaView style={{flex:1}}>
        <View style={{ height:100, backgroundColor: '#9FA8DA' }}>
          <Image
                style={{marginLeft:20,height:100,width:100,borderRadius:50}}
                source={require('./assets/puppy.jpg')}/>
        </View>

        <View style={{flexDirection:'row', margin:20, alignItems:'center'}}>
    //////here is where I get an error
        <TouchableOpacity
            style={{marginTop:0}}
            onPress={()=> {this.props.navigation.navigate('profile')}}>
          <Text style={{fontSize:18, fontWeight:'bold'}}>navigate</Text>
          <Image
                style={{height:12,width:12}}
                source={require('./assets/drawable-hdpi/ic_arrow_depth.png')}/>
        </TouchableOpacity>
        </View>
        <ScrollView>
          <DrawerItems {...props}/>
        </ScrollView>
      </SafeAreaView>
      )
    }

const AppDrawerNavigator = createDrawerNavigator({
    one: AppStackNavigator,
    two: BoardScreen,
    three: NotificationScreen,
  },{
    contentComponent:CustomDrawerContentComponent,
  })

const AppStackNavigator = createStackNavigator({
  profile: {
  screen: profileScreen,
  navigationOptions: {
      header: null
    },
  },
})

const StartSwitchNavigator = createSwitchNavigator(
    {
      App: AppDrawerNavigator,
    },
    {
      initialRouteName: 'App',
    }
  )

enter image description here

最佳答案

您的 CustomDrawerContentComponent 组件是功能组件。直接使用props.navigation代替this.props

关于javascript - react 导航如何从自定义抽屉导航导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53757316/

相关文章:

javascript - 为什么我的自定义 Google Analytics 事件没有触发?

javascript - 为什么一个空的、默认的 NextJS 应用程序的性能分数这么低?

javascript - 一台特定计算机的 Ajax 请求数据未定义/失败

react-native - 为什么多个WebView不能共享缓存?

reactjs - 如何使用 react 导航在页面之间传递状态?

javascript - Firebase:如何在 HTTP 函数下动态包含模块

react-native - react native : Not smooth scrolling with DrawerNavigator

reactjs - 点击通知后应用程序未打开 react native firebase

react-native - react 原生中 TextInput 上的 PanResponder

reactjs - 使用渲染外部的堆栈导航访问 channel 数据