1
0
Fork 0
tsgames/src/common/hooks/useInputState.ts

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];
}