im创建一个带有ReactNavigation和Redux的React-Native应用程序。
在加载应用程序时,我从Navcontainer那里得到以下警告:Warning: Failed prop type: The prop 'navigation.dispatch' is marked as required in 'CardStack', but its value is 'undefined'
当我尝试通过 this.props.navigation.navigate('Station');
导航页面作为navigation.dispatch不是函数
该应用程序具有Navcontainer,我猜该应用程序缺少某些内容,因为渲染函数中的调度不确定:
import React, { Component } from 'react'
import { addNavigationHelpers } from 'react-navigation'
import AppNavigator from '../lib/navigationConfiguration'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux';
import { ActionCreators } from '../actions';
class NavContainer extends Component {
render(){
const { dispatch, navigationState} = this.props
return (
<AppNavigator
navigation={
addNavigationHelpers({
dispatch: dispatch,
state: navigationState
})
}
/>
)
}
}
function mapStateToProps(state){
return {
navigationState: state.nav
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators(ActionCreators, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(NavContainer)
定义 mapDispatchToProps
时, connect
不会将 dispatch
传递给包装组件。这就是为什么 this.props.dispatch
不确定。要解决这个问题,您可以做:
function mapDispatchToProps(dispatch){
return Object.assign({dispatch: dispatch}, bindActionCreators(ActionCreators, dispatch));
}