You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
By clicking “Sign up for GitHub”, you agree to our
terms of service
and
privacy statement
. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
我的某个组件使用Taro.eventBus.trigger(),触发‘updateHomeData’事件,本组件监听这个事件,然后在'updateHomeData'事件里执行this.setState()改变state变量。然后在setState()的回调里调用某个函数。但奇怪的是setState()的回调不执行。
代码如下图:
我想用setTimeout模拟setState的回调函数,居然发现一个更加奇怪的现象。
我在setState后面加上setTimeout定时器后,居然setState的回调就执行了。这到底是什么情况?
代码如下:
打印的结果如下:
求大佬搭救一下
欢迎提交 Issue~
如果你提交的是 bug 报告,请务必遵循
Issue 模板
的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏
如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。
Good luck and happy coding~
------------------ 原始邮件 ------------------
发件人: Yu Che <notifications@github.com>
发送时间: 2019年4月22日 19:24
收件人: NervJS/taro <taro@noreply.github.com>
抄送: hello-lun <872570730@qq.com>, Author <author@noreply.github.com>
主题: 回复:[NervJS/taro] this.setState()函数的回调事件不执行 (
#2855
)
参考一下 Taro UI 的写法?
bindMessageListener () {
Taro.eventCenter.on('atMessage', (options = {}) => {
const { message, type, duration } = options
const newState = {
_isOpened: true,
_message: message,
_type: type,
_duration: duration || this.state._duration
this.setState(newState, () => {
clearTimeout(this._timer)
this._timer = setTimeout(() => {
this.setState({
_isOpened: false
}, this.state._duration)
// 绑定函数
Taro.atMessage = Taro.eventCenter.trigger.bind(Taro.eventCenter, 'atMessage')
https://github.com/NervJS/taro-ui/blob/b63eb1aebb366046e3416ab806e76a6f3bddca43/src/components/message/index.js#L19-L39
@hello-lun 复现不了。可以升级到 1.2.27-beta.0 再试试。
import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
export default class Detail extends Component {
state = {
current: 0
componentWillMount () {
Taro.eventCenter.on('fire', () => {
this.setState({
current: 1
}, () => {
console.log('cb called')
render() {
return (
<View>current: {this.state.current}</View>
<View onClick={() => Taro.eventCenter.trigger('fire')}>fire</View>
</View>
Hello~
您的问题我们无法复现。如果有空的话还请拔冗提供一个简单的复现 demo,否则这个 issue 将在 15 天后被自动关闭。
如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。
Good luck and happy coding~