Skip to content

useKeyPressEnter

Hooks to help creating key press handler

Abui

Created by / Abui

TypeScript Implementation

TS
import { KeyboardEvent } from 'react'

const useKeyPressEnter = (onPressEnter: () => void) => {
  function keyHandler(event: KeyboardEvent<HTMLDivElement>): void {
    if (event.key === 'Enter') {
      if (onPressEnter) onPressEnter()
    }
  }
  return keyHandler
}

export { useKeyPressEnter }

Usage

TSX
const handleKeyPress = useKeyPressEnter(() => {
  console.log('Bamm')
})