当我按下按钮时没有调用方法

编程入门 行业动态 更新时间:2024-10-25 20:30:34
本文介绍了当我按下按钮时没有调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 React 导航并在右侧添加了一个按钮以使用默认导航选项从我的应用程序注销,如下所示:

I am using react navigation and have added a button on the right to signout from my app using default navigation options as shown below :

const otherApp = createStackNavigator({ Welcome : { screen : WelcomeScreen } }, { defaultNavigationOptions : ({navigation}) => ({ title : 'Welcome', headerStyle: { backgroundColor: '#29434e', shadowColor: 'transparent', elevation: 0 }, headerRight: ( <TouchableOpacity style={{ backgroundColor: '#DDDDDD', padding: 5 }} onPress={() => navigation.getParam('logout')}> <Text style={{ fontSize: 10, }}> Logout </Text> </TouchableOpacity> ), }) });

我正在绑定要调用的方法,如下所示:

And i am binding the method to be invoked as follows :

_Logout() { this.props.signOut(); } componentDidMount(){ this.props.navigation.setParams({ logout : this._Logout.bind(this) }) }

使用 redux 将函数映射到 props

Function is maped to props using the redux

const mapDispatchToProps = (dispatch) => { return { Signout : ()=> dispatch(Signout()) } }

但问题是当我按下按钮时,它没有调用该方法!

But the problem is when i press the button, it does not invoke the method !

推荐答案

我得到了解决方案,我做错了什么,我没有在按钮的 onPress 中传递回电!

I got the solution, what i was doing wrong that i wasn't passing a call back in the onPress of the button !

logoutFromFirebase = () => { this.props.Signout(); this.props.navigation.navigate(this.props.user.uid ? 'App' : 'Auth') } componentDidMount(){ this.props.navigation.setParams({ logout : this.logoutFromFirebase }) }

和默认导航

defaultNavigationOptions : ({navigation}) => ({ title : 'Welcome', headerStyle: { backgroundColor: '#29434e', shadowColor: 'transparent', elevation: 0 }, headerRight: ( <TouchableOpacity style={{ backgroundColor: '#DDDDDD', padding: 5 }} onPress={navigation.getParam('logout' , () => Reactotron.log('Logout not callled'))}> <Text style={{ fontSize: 10, }}> Logout </Text> </TouchableOpacity> ), })

现在它工作正常!

更多推荐

当我按下按钮时没有调用方法

本文发布于:2023-10-31 07:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   按下   按钮   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!