Skip to content

Commit

Permalink
fix(react): 修复 react 不能取消事件监听的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Jan 14, 2021
1 parent f9b034a commit fb057d3
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/taro-react/src/props.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TaroElement, Style, document, FormElement } from '@tarojs/runtime'
import { TaroElement, Style, FormElement } from '@tarojs/runtime'
import { isFunction, isString, isObject, isNumber, internalComponents, capitalize, toCamelCase } from '@tarojs/shared'
import { CommonEvent } from '@tarojs/components'

export type Props = Record<string, unknown>

Expand All @@ -26,11 +25,11 @@ export function updateProps (dom: TaroElement, oldProps: Props, newProps: Props)
}
}

function eventProxy (e: CommonEvent) {
const el = document.getElementById(e.currentTarget.id)
const handlers = el!.__handlers[e.type]
handlers[0](e)
}
// function eventProxy (e: CommonEvent) {
// const el = document.getElementById(e.currentTarget.id)
// const handlers = el!.__handlers[e.type]
// handlers[0](e)
// }

function setEvent (dom: TaroElement, name: string, value: unknown, oldValue?: unknown) {
const isCapture = name.endsWith('Capture')
Expand All @@ -47,7 +46,7 @@ function setEvent (dom: TaroElement, name: string, value: unknown, oldValue?: un

if (isFunction(value)) {
if (!oldValue) {
dom.addEventListener(eventName, eventProxy, isCapture)
dom.addEventListener(eventName, value, isCapture)
}
if (eventName === 'regionchange') {
dom.__handlers.begin[0] = value
Expand All @@ -56,7 +55,7 @@ function setEvent (dom: TaroElement, name: string, value: unknown, oldValue?: un
dom.__handlers[eventName][0] = value
}
} else {
dom.removeEventListener(eventName, eventProxy)
dom.removeEventListener(eventName, oldValue as any)
}
}

Expand Down

0 comments on commit fb057d3

Please sign in to comment.