shadcn-ahooks

useEventTarget

A hook that encapsulates onChange and value logic for form controls that obtains value through event.target.value. It also supports custom transformer and reset functionalities.

Overview

A hook that encapsulates onChange and value logic for form controls that obtains value through event.target.value. It also supports custom transformer and reset functionalities.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useEventTarget.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useEventTarget.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useEventTarget.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useEventTarget.json

A hook that encapsulates onChange and value logic for form controls that obtains value through event.target.value. It also supports custom transformer and reset functionalities.

Example

Basic Usage

Custom transformer

API

const [value, { onChange, reset }] = useEventTarget<T, U>(Options<T, U>);

Result

PropertyDescriptionType
valuecomponent valueT
onChangecallback when value changes(e: { target: { value: T } }) => void
resetfunction to reset the value to initialValue() => void

Options

PropertyDescriptionTypeDefault
initialValueinitial valueT-
transformercustom transform function applied to value(value: U) => T-

On this page