如何在 React-Native 的事件中调用两个函数? [英] How to call Two functions in a event on React-Native?

查看:30
本文介绍了如何在 React-Native 的事件中调用两个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尝试:Source(React-Native 事件中的两个函数)

onValueChange={(selected) =>{this.setState({selected});this.state.eventOnChange();}}

但这不行

执行第一个函数,但不执行第二个.

解决方案

Component.setState() 是异步的,可能会在第二次调用时被锁定,同时它仍在执行第一次调用.

在回调中执行第二次调用,如下所示:

this.setState({selected},() => {this.state.eventOnChange();});

I would try : Source ( Two function in a event on React-Native )

onValueChange={(selected) => { this.setState({selected}); this.state.eventOnChange(); }}

But that's Not work

That executes the first function , but not the second.

解决方案

Component.setState() is asynchronous and may be locked on the second call while it is still doing the first.

Do the second call in a callback like this:

this.setState({selected},() => {
    this.state.eventOnChange();
});

这篇关于如何在 React-Native 的事件中调用两个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆