React native 如何从静态导航中调用this.props选项-反应导航

React native 如何从静态导航中调用this.props选项-反应导航,react-native,react-navigation,stack-navigator,React Native,React Navigation,Stack Navigator,我试图在堆栈导航器标题中的On Press of按钮中调用handlelogout(),然后在handlelogout()中调用this.props.logout操作,该操作将调用导航减速机重置为登录屏幕…...this.props.logout不调用操作…什么都没有发生 static navigationOptions = ({ navigation }) => { const { params } = navigation.state; console.log("nav

我试图在堆栈导航器标题中的On Press of按钮中调用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 })} />
    }
  };

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

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

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

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 />
    }
  };
import ButtonForLogout from./test/ButtonForLogout';
...
静态导航选项=({navigation})=>{
const{params}=navigation.state;
控制台日志(“导航”,导航);
返回{
标题:“配置文件列表”,
手势已启用:错误,
headerLeft:null,
头灯:
}
};
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);
import React,{Component}来自'React';
从“react native”导入{View,Button};
从'react redux'导入{connect};
从“/youractions”导入{ReduxActions};
类ButtonForLogout扩展组件{
render(){
返回(
);
}
}
常量mapStateToProps=state=>({});
导出默认连接(MapStateTops、ReduxActions)(ButtonForLogout);

类似于此。

不确定这是实际问题还是问题代码片段中的错误,但您没有调用
this.props.logout
方法。为此,您需要使用
this.props.logout()
调用函数。jevakallio this.props.logout(reduxactions)是对actioncreator的分派操作,它调用导航缩减器。。。。。
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);