Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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 3.6.26 View 初始绑定 onClick 为 undefined,点击报错 #15530

Closed
shushu2013 opened this issue Apr 18, 2024 · 0 comments · Fixed by #15540 or #15699
Closed

Taro 3.6.26 View 初始绑定 onClick 为 undefined,点击报错 #15530

shushu2013 opened this issue Apr 18, 2024 · 0 comments · Fixed by #15540 or #15699
Labels
F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x

Comments

@shushu2013
Copy link
Contributor

shushu2013 commented Apr 18, 2024

相关平台

H5

浏览器版本: chrome
使用框架: React

复现步骤

import React from 'react'
import { View } from '@tarojs/components'
import './index.styl'

function Index() {

  const [count, setCount] = React.useState(0)

  return (
    <View className="nutui-react-demo">
      <View
        className="custom-btn"
        onClick={count >= 0 ? undefined : () => {
          setCount(count  + 1)
        }}
      >
        {count}
      </View>
    </View>
  )
}

export default Index

View 初始绑定 onClick 为 undefined,点击报错

期望结果

View 初始绑定 onClick 为 undefined,点击不报错

实际结果

View 初始绑定 onClick 为 undefined,点击报错

环境信息

👽 Taro v3.6.26

  Taro CLI 3.6.26 environment info:
    System:
      OS: macOS 12.1
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 16.20.0 - ~/.nvm/versions/node/v16.20.0/bin/node
      Yarn: 1.22.19 - ~/.nvm/versions/node/v16.20.0/bin/yarn
      npm: 8.19.4 - ~/.nvm/versions/node/v16.20.0/bin/npm
    npmPackages:
      @tarojs/cli: 3.6.26 => 3.6.26 
      @tarojs/components: 3.6.26 => 3.6.26 
      @tarojs/helper: 3.6.26 => 3.6.26 
      @tarojs/mini-runner: 3.6.26 => 3.6.26 
      @tarojs/plugin-framework-react: 3.6.26 => 3.6.26 
      @tarojs/plugin-html: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-alipay: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-h5: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-jd: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-qq: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-swan: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-tt: 3.6.26 => 3.6.26 
      @tarojs/plugin-platform-weapp: 3.6.26 => 3.6.26 
      @tarojs/react: 3.6.26 => 3.6.26 
      @tarojs/runtime: 3.6.26 => 3.6.26 
      @tarojs/shared: 3.6.26 => 3.6.26 
      @tarojs/taro: 3.6.26 => 3.6.26 
      @tarojs/webpack-runner: 3.6.26 => 3.6.26 
      babel-preset-taro: 3.6.26 => 3.6.26 
      eslint-config-taro: 3.6.26 => 3.6.26 
      react: ^18.0.0 => 18.0.0 
    npmGlobalPackages:
      typescript: 5.0.4

补充信息

报错原因

attachProps.ts 代码中,未判断 newEventHandler 不存在的情况,绑定报错

    const listener: EventCenter.EventCallback = eventStore[eventName] = function (e: Event) {
      // React batch event updates
      unstable_batchedUpdates(() => newEventHandler.call(this, e))
      // 控制是否更新受控组件的 value 值
      finishedEventHandler(node)
    }
    listener.fn = newEventHandler
    node.addEventListener(
      eventName,
      listener
    )
  }

export const syncEvent = (
node: HTMLElementWithEvents,
eventName: string,
newEventHandler?: (e: Event) => any
) => {
const eventStore = node.__events ||= {}
const oldEventHandler = eventStore[eventName]
if (!newEventHandler && oldEventHandler) {
node.removeEventListener(eventName, oldEventHandler)
} else {
if (oldEventHandler) {
if (oldEventHandler.fn === newEventHandler) {
return
} else {
// 删除旧的,绑定新的
node.removeEventListener(eventName, oldEventHandler)
}
}
const listener: EventCenter.EventCallback = eventStore[eventName] = function (e: Event) {
// React batch event updates
unstable_batchedUpdates(() => newEventHandler.call(this, e))
// 控制是否更新受控组件的 value 值
finishedEventHandler(node)
}
listener.fn = newEventHandler
node.addEventListener(
eventName,
listener
)
}
}

@taro-bot2 taro-bot2 bot added F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x labels Apr 18, 2024
@github-project-automation github-project-automation bot moved this to Padding in H5 Apr 18, 2024
This was referenced Apr 20, 2024
@ZakaryCode ZakaryCode moved this from Padding to 组件 in H5 May 2, 2024
@github-project-automation github-project-automation bot moved this from 组件 to Done in H5 May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x
Projects
Archived in project
1 participant