18 lines
571 B
TypeScript
18 lines
571 B
TypeScript
import { useCallback, useState } from "preact/hooks";
|
|
|
|
export const useInputState = (defaultValue = ''): [string, (e: Event | string) => void] => {
|
|
const [value, setValue] = useState(defaultValue);
|
|
|
|
const handleInput = useCallback((e: Event | string) => {
|
|
if (typeof e === 'string') {
|
|
setValue(e);
|
|
} else {
|
|
const { target } = e;
|
|
if (target && 'value' in target && typeof target.value === 'string') {
|
|
setValue(target.value);
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
return [value, handleInput];
|
|
} |