quark-design icon indicating copy to clipboard operation
quark-design copied to clipboard

fix(react): prevent double invoking

Open vaakian opened this issue 3 years ago • 6 comments

closes #56

vaakian avatar Nov 02 '22 16:11 vaakian

It maybe call many times, use Set.has() to judge seems better, likes:

const nativeEvents = /* #__PURE__ */ Object.keys(HTMLElement.prototype)
  .filter(eventName => eventName.startsWith('on'))
  .map(eventName => eventName.slice(2));

const nativeEventsSet = new Set(nativeEvents)

export function isNativeEvent(eventName: string) {
  return nativeEventsSet.has(eventName)
};

Maidang1 avatar Nov 02 '22 18:11 Maidang1

Good idea, I've just added it.

vaakian avatar Nov 03 '22 02:11 vaakian

In quark, such as dialog、radio has custom event 'close'、'change', const nativeEvents = /* #__PURE__ */ Object.keys(HTMLElement.prototype) .filter(eventName => eventName.startsWith('on')) .map(eventName => eventName.slice(2));

nativeEvents alse has chaneg, this will filte quark custom event.

sanqi-med avatar Nov 04 '22 03:11 sanqi-med

In quark, such as dialog、radio has custom event 'close'、'change', const nativeEvents = /* #__PURE__ */ Object.keys(HTMLElement.prototype) .filter(eventName => eventName.startsWith('on')) .map(eventName => eventName.slice(2));

nativeEvents alse has chaneg, this will filte quark custom event.

As I mentioned before, those events will be registered/updated by React itself when passing props to it.

https://github.com/hellof2e/quark-design/blob/a4c4b7f0b538b2d723a98c0809b156cbff154899/packages/quark-reactify/src/index.ts#L63

those onChange / onClick / onInput events will be passed to React in ...rest and then be registered.

The core logic: All native events will be register by React. Custom events should be register by quark manually using this.setEvent(...).

vaakian avatar Nov 04 '22 03:11 vaakian

I've tested <Field> component.

This onFocus will also be triggered twice, because it matches native event.

<Field onFocus={(e) => console.log(e)}></Field>

~~But onChange don't trigger at all, so this is a bug from Field component itself. (should also be triggered twice as onFocus)~~

<Field onChange={(e) => console.log(e)}></Field>

I found that there are more to deal with, I'll try to fix this later.

vaakian avatar Nov 04 '22 03:11 vaakian

The core logic: All native events will be register by React. Custom events should be register by quark manually using this.setEvent(...).

@vaakian you are right。quark now has filter click、 focus、 blur ,other native event I think can not need be use in use quark.which scen can need use another native event ?

sanqi-med avatar Nov 04 '22 05:11 sanqi-med

I will close this pr. if need, you can reopen it, thank you!

sanqi-med avatar Nov 08 '22 07:11 sanqi-med