mergeProps chaning events twice #6706
-
guys i was converting react-aria's mergeProps hook for use in svelte, i did this code export const mergeProps = <T extends PropsArg[]>(
...args: T
): UnionToIntersection<TupleTypes<T>> => {
const result: Props = { ...args[0] };
for (const props of args) {
for (const key in props) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const a = result[key];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const b = props[key];
if (typeof a === 'function' && typeof b === 'function' && key.startsWith('on')) {
result[key] = chain(a as CallableFunction, b as CallableFunction);
} else if (key === 'class' && typeof a === 'string' && typeof b === 'string') {
result[key] = clsx(a, b);
} else if (key === 'id' && typeof a === 'string' && typeof b === 'string') {
result.id = mergeIds(a, b);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
result[key] = b !== undefined ? b : a;
}
}
}
return result as UnionToIntersection<TupleTypes<T>>;
}; its chaining a event twice, like one handler runs once but other is runnign twice, i have checked by logging props, before merging props are () => console.log('pressed') () => console.log("F pressed") and after merging (...args) => {
for (const callback of callbacks) {
if (typeof callback === "function") {
callback(...args);
}
}
} which logs pressed
pressed
F pressed pls help , how do i fix this |
Beta Was this translation helpful? Give feedback.
Answered by
snowystinger
Jul 16, 2024
Replies: 1 comment 1 reply
-
I'm so sorry to be the bearer of this news, you have an off by 1 error. You're double including where i = 1 instead of 0
Can't say how many times I've done that too and been frustrated for hours. Hope you're able to get back on track quickly now! |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
NaviTheCoderboi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm so sorry to be the bearer of this news, you have an off by 1 error. You're double including
args[0]
react-spectrum/packages/@react-aria/utils/src/mergeProps.ts
Line 40 in 4fc7017
where
i = 1
instead of0
Can't say how many times I've done that too and been frustrated for hours. Hope you're able to get back on track quickly now!