bigbuffet-rw/app/soapbox/hooks/forms/useTextField.ts
2023-03-29 20:14:41 -05:00

23 lines
No EOL
524 B
TypeScript

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