代码之家  ›  专栏  ›  技术社区  ›  ZedTuX

反应导航:未定义的导航属性

  •  0
  • ZedTuX  · 技术社区  · 6 年前

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

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

    这个 HomeScreenContainer DocumentScanScreenContainer 是必需的,因为 反应式导航 仅接受 React.Component 和我的 HomeScreen DocumentScanScreen 组件是Redux组件,直接导入会使 反应式导航 引发错误。

    主屏幕控制器 文档扫描屏幕容器 是相似的,所以这里是 文档扫描屏幕容器 :

    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} />
        )
      }
    }
    

    最后是一个简短的版本 文档扫描屏幕 :

    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:道具类型 航行 是无效的;它必须是一个函数,通常来自 prop-types 包装,但已收到 undefined .

    是我做错了,还是有办法通过,从路由器, 航行 支持 文档扫描屏幕容器 ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   NULL SWEΔT    6 年前

    试试这个:

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

    *我不确定这是否可行,但我无法添加评论,因为我有50个代表