bigbuffet-rw/app/soapbox/hooks/forms/useTextField.ts

23 lines
524 B
TypeScript
Raw Normal View History

2023-03-29 18:14:41 -07:00
import { useEffect, useState } from 'react';
function useTextField(initialValue: string | undefined) {
const [value, setValue] = useState(initialValue);
const hasInitialValue = typeof initialValue === 'string';
const onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement> = (e) => {
setValue(e.target.value);
};
useEffect(() => {
if (hasInitialValue) {
setValue(initialValue);
}
}, [hasInitialValue]);
return {
value,
onChange,
};
}
export { useTextField };