reactjs - dispatch 不是函数错误
问题描述
因此,我已确保connect
我mapDispatchToProps
正确并正确绑定了功能(我认为),并确保没有错字。
但由于某种原因,以下代码给我调度不是函数错误:
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 上的注销按钮。
谁能帮我指出我在这里错过了什么?
提前致谢!:)
解决方案
尝试:
export default connect(null, mapDispatchToProps) (DrawNav);
mapDispatchToProps
应该是您connect
方法的第二个参数。
提供给第一个函数参数的参数connect
将是state
,并且将给出第二个参数函数dispatch
在您的示例中,因为 yourmapDispatchToProps
是 中的第一个参数connect
,所以它作为参数接收state
。然后,您bindActionCreators
将尝试将其作为引发错误的函数调用。
推荐阅读
- r - 构建向量的所有组合 - 寻找更好的方法
- java - 编译最新版本的l2switch时出现问题
- ios - Swift NSCoder 不适用于双变量
- java - Java 等价于 C++ 箭头
- sqlite - SQLite如何处理最后带有负号的负数
- javascript - FeatureCollection 中的“crs”是什么意思?
- javascript - firebase 实时数据库,如果用户是对话的一部分,则 child_changed
- ios - iOS:应用 API KEY 限制时 Google Maps API 失败
- excel - Excel - 查找第二高块平均值
- javascript - JavaScript 性能:对于大量不变的数据,使用对象变量还是 AJAX 请求更好?