undefined不是一个函数(评估'navigation.dispatch')



我正在尝试在用户注销后导航到第一页! 并收到此错误:

undefined不是一个函数(评估'navigation.dispatch'(。

我的操作创建器代码如下所示:

import {NavigationActions} from 'react-navigation';
import {AsyncStorage} from 'react-native';
import {applyMiddleware as dispatch} from "redux";
export const userRemove = (navigation) => {
AsyncStorage.removeItem('user');
const navigateAction = NavigationActions.navigate({
routeName: 'First',
props: {}
});
navigation.dispatch(navigateAction);
};

我已经从组件的侧边栏调用了此操作。组件代码是正确的,因为 AsyncStorage.removeItem('user'( 工作正常! 对不起,语法不好。我不能在这里用英语写比这条错误消息更好的

了我的组件代码在这里:

import React, {Component} from 'react';
import {
Container,
Content,
Footer,
FooterTab,
Button,
Icon
} from 'native-base';
import {userRemove} from '../actions/sideBarAction';
import {connect} from "react-redux";
import {bindActionCreators} from "redux";
class SideBar extends Component {
constructor(props) {
super(props);
}
logout() {
userRemove(this.props.navigation);
}
render() {
return (
<Container>
<Content>
...
</Content>
<Footer>
<FooterTab>
<Button onPress={this.logout.bind(this)}>
<Icon name="logout"/>
</Button>
</FooterTab>
</Footer>
</Container>
)
}
}
const mapStateToProps = state => {
return {
username: state.home.username,
password: state.home.password,
score: state.home.score,
}
};
const mapDispatchToProps = dispatch => {
return Object.assign({dispatch: dispatch}, 
bindActionCreators(userRemove, dispatch));
};
connect(mapStateToProps, mapDispatchToProps)(SideBar);

您的组件是否已添加到主导航器中?如果没有,请查看是否可以添加。

您还可以使用react-navigation中的withNavigation函数包装组件。这将确保您拥有有效的navigation道具。

import { withNavigation } from react-navigation;
...
connect(mapStateToProps, mapDispatchToProps)(withNavigation(SideBar));

或者,您也可以从父组件传递this.props.navigation

相关内容

最新更新