JavaScript events

🍱Framework
javascript

event handling

Learned that every extra event listener (if not removed) will trigger new event —> They can multiply if applied wrong and cause app to lag!

useEffect(() => {
  const buttonRef = innerRef.current;

  if (Platform.OS === 'web') {
    (buttonRef as HTMLDivElement | null)?.addEventListener('keydown', handleKeyDown);
  }
  return () =>
    (buttonRef as HTMLDivElement | null)?.removeEventListener('keydown', handleKeyDown);
}, [handleKeyDown, innerRef]);

Discuss on TwitterImprove this article: Edit on GitHub

Discussion


Explain Programming

André Kovac builds products, creates software, teaches coding, communicates science and speaks at events.