如何从静态navigationOptions - react-navigation调用this.props

时间:2017-12-21 11:16:55

标签: react-native react-navigation stack-navigator

我试图在堆栈导航器的标题中的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函数

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

i am attaching the log i printed in the console

这里我将logout绑定到mapdispatchprops

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

1 个答案:

答案 0 :(得分: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);

类似的东西。