zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【taro react】----打包微信小程序后 onShow 多次执行(随着进入页面的次数在不断的累计)

打包微信执行程序React 页面 ---- 进入
2023-09-14 09:15:07 时间

页面子组件 onShow 执行多次【累计执行】

1. 出现问题的代码

test.jsx 测试组件代码

import React, { Component } from 'react'
import { View } from '@tarojs/components'
import { eventCenter, getCurrentInstance } from '@tarojs/taro'

export default class RuiPoster extends Component {
  constructor(props) {
    super(props)
  }
  state = {
    count: 0
  }
  $instance = getCurrentInstance();
  componentDidMount(){
    const onShowEventId = this.$instance.router.onShow;
    eventCenter.on(onShowEventId, this.onShow);
  }
  onShow = () => {
    console.log('进入页面第几次:',++this.state.count)
    console.log('onshow', +new Date())
  }
  render () {
    return (
      <View>
        
      </View>
    )
  }
}

2. 代码执行结果