0

我正在尝试在堆栈导航器标题中的按钮 onPress 中调用 handlelogout(),然后在 handlelogout() 中调用this.props.logout操作,该操作将调用导航减速器以重置到登录屏幕.....但this.props.logout没有调用一个动作....什么也没发生

static navigationOptions = ({ navigation }) => {
    const { params } = navigation.state;
    console.log("navigation", navigation);

    return {
      title: "Profile List",
      gesturesEnabled: false,
      headerLeft: null,
      headerRight: <Button title={"Logout"} onPress={() => params.handlelogout && params.handlelogout({ state: navigation.state })} />
    }
  };

这是我的句柄注销功能

handlelogout(state) {
    console.log("in handlelogout", this.props.logout, state);
    this.props.logout;
  }

我正在附加我在控制台中打印的日志

在这里,我将注销绑定到 mapdispatchprops

function mapDispatchToProps(dispatch) {
  return bindActionCreators(ReduxActions, dispatch);
}
4

1 回答 1

1

您可以尝试将按钮放在另一个元素中,然后从该元素/类处理您的注销。

import ButtonForLogout from './test/buttonforlogout';

...

static navigationOptions = ({ navigation }) => {
    const { params } = navigation.state;
    console.log("navigation", navigation);

    return {
      title: "Profile List",
      gesturesEnabled: false,
      headerLeft: null,
      headerRight: <ButtonForLogout />
    }
  };

buttonforlogout.js

import React, { Component } from 'react';
import { View, Button } from 'react-native';
import { connect } from 'react-redux';
import { ReduxActions } from './youractions';

class ButtonForLogout extends Component {

   render(){
   return(
      <View>
       <Button title={"Logout"} onPress={this.props.logout} />
      </View>
   );
   }

}

const mapStateToProps = state => ({});

export default connect(mapStateToProps, ReduxActions)(ButtonForLogout);

类似的东西。

于 2017-12-22T02:51:45.910 回答