我有一个像这样的react-navigation
路由器:
const RootNavigator = createSwitchNavigator({
App: createBottomTabNavigator({
Home: {
screen: HomeScreenContainer
},
Scan: {
screen: DocumentScanScreenContainer
},
// ...
}, {
tabBarOptions: {
showLabel: false,
// ...
}
})
})
HomeScreenContainer
和DocumentScanScreenContainer
是必需的,因为react-navigation
只接受React.Component
,而我的HomeScreen
和DocumentScanScreen
组件是Redux组件并直接导入它们使react-navigation
抛出错误。
HomeScreenContainer
和DocumentScanScreenContainer
是相似的,所以这里是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
?
试试这个:
Scan: {
screen: (props) => <DocumentScanScreenContainer {...props} />
},
*我不确定这是否有效,但我不能添加评论,因为我有<50代表