首页 > 解决方案 > dispatch 不是函数错误

问题描述

因此,我已确保connectmapDispatchToProps正确并正确绑定了功能(我认为),并确保没有错字。

但由于某种原因,以下代码给我调度不是函数错误:

import React from 'react';
import {View, Button} from 'react-native';
import { DrawerNavigator, DrawerItems } from 'react-navigation';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import aboutScreen from './About';
import settingScreen from './Setting';
import Home from './Home';
import Login from '../../auth/scenes/Login';
import * as authAction from '../../auth/actions';
import * as homeAction from '../actions';

export const mapDispatchToProps = (dispatch) => ({
  actionsHome: bindActionCreators(homeAction, dispatch),
  actionsAuth: bindActionCreators(authAction, dispatch)
});

const CustomDrawerContentComponent = (props) => (
  <View>
     <DrawerItems {...props}/>
     <Button title="Logout" onPress={()=>props.screenProps()}/>
  </View>
);

const Drawer = DrawerNavigator({
    Home: {
      screen: Home
    },
    About: {
      screen: aboutScreen
    },
    Setting:{
      screen: settingScreen
    },
  },
  {
    initialRouteName: 'Home',
    contentComponent: CustomDrawerContentComponent,
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle'
  }
);

class DrawNav extends React.Component{  
  constructor(){
    super();
    this.onSignOut = this.onSignOut.bind(this);
  }

  onSuccess() {
    this.props.actionsHome.successSignOut();
    Actions.reset("Auth");
  }

  onError(error) {
    Alert.alert('Oops!', error.message);
  }

  onSignOut() {
    this.props.actionsAuth.signOut(this.onSuccess.bind(this),this.onError.bind(this))
  }

  render(){
      return <Drawer screenProps = {this.onSignOut}/>;  
  }
}

export default connect(mapDispatchToProps) (DrawNav);

我在我的 Home 组件上尝试了类似的设置并且它有效。这几乎是 Home 注销示意图的复制粘贴,因为我需要在我的抽屉上而不是 Home 上的注销按钮。

谁能帮我指出我在这里错过了什么?

提前致谢!:)

标签: reactjsreact-nativereduxreact-reduxdispatch

解决方案


尝试: export default connect(null, mapDispatchToProps) (DrawNav);

mapDispatchToProps应该是您connect方法的第二个参数。

提供给第一个函数参数的参数connect将是state,并且将给出第二个参数函数dispatch

在您的示例中,因为 yourmapDispatchToProps是 中的第一个参数connect,所以它作为参数接收state。然后,您bindActionCreators将尝试将其作为引发错误的函数调用。


推荐阅读