react-navigation:未定义的导航道具

问题描述 投票:0回答:1

我有一个像这样的react-navigation路由器:

const RootNavigator = createSwitchNavigator({
  App: createBottomTabNavigator({
    Home: {
      screen: HomeScreenContainer
    },
    Scan: {
      screen: DocumentScanScreenContainer
    },
    // ...
  }, {
    tabBarOptions: {
      showLabel: false,
      // ...
    }
  })
})

HomeScreenContainerDocumentScanScreenContainer是必需的,因为react-navigation只接受React.Component,而我的HomeScreenDocumentScanScreen组件是Redux组件并直接导入它们使react-navigation抛出错误。

HomeScreenContainerDocumentScanScreenContainer是相似的,所以这里是DocumentScanScreenContainer

import React from 'react'
import PropTypes from 'prop-types'

import DocumentScanScreen from '../../screens/DocumentScanScreen'

export default class DocumentScanScreenContainer extends React.Component {
  static propTypes = {
    navigation: PropTypes.shape.isRequired
  }

  render() {
    const { navigation } = this.props

    // Passing the navigation object to the screen so that you can call
    // this.props.navigation.navigate() from the screen.
    return (
      <DocumentScanScreen navigation={navigation} />
    )
  }
}

最后是DocumentScanScreen的简短版本:

import React from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'

class DocumentScanScreen extends React.Component {
  static propTypes = {
    token: PropTypes.string,
    navigation: PropTypes.shape.isRequired
  }

  componentDidMount() {
    const { token, navigation } = this.props
    if (token === undefined || token === null || token === 0) {
      navigation.navigate('Authentication')
    }
  }

  // ...
}

我在每个级别都有警告,说明navigation未定义,所以就像我的DocumentScanScreenContainer没有从路由器接收navigation道具:

警告:失败的道具类型:DocumentScanScreenContainer:道具类型navigation无效;它必须是一个函数,通常来自prop-types包,但收到undefined

我做错了还是有办法从路由器传递navigation道具到DocumentScanScreenContainer

react-native react-navigation react-props
1个回答
0
投票

试试这个:

Scan: {
  screen: (props) => <DocumentScanScreenContainer {...props} />
},

*我不确定这是否有效,但我不能添加评论,因为我有<50代表

© www.soinside.com 2019 - 2024. All rights reserved.