Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigation.dispatch is undefined in reactnavigation

Im creating a react-native app with reactnavigation and redux.

On loading the app I get the following warning from NavContainer: Warning: Failed prop type: The prop 'navigation.dispatch' is marked as required in 'CardStack', but its value is 'undefined'

This leads to errors when I attempt to navigate pages via this.props.navigation.navigate('Station'); as navigation.dispatch is not a function

The app has a NavContainer which I'm guessing is missing something as dispatch is undefined in the render function:

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)
like image 912
Dan Avatar asked Feb 24 '17 06:02

Dan


1 Answers

When you define a mapDispatchToProps, connect doesn't pass dispatch to the wrapped component.. that's why this.props.dispatch is undefined. To solve this, you could do:

function mapDispatchToProps(dispatch){
  return Object.assign({dispatch: dispatch}, bindActionCreators(ActionCreators, dispatch));
}
like image 71
Lucas Avatar answered Oct 12 '22 23:10

Lucas