커스텀 훅
interface IProps {
handleSubmit: (
e:
| React.FormEvent<HTMLFormElement>
| React.KeyboardEvent<HTMLTextAreaElement>
) => void;
isSubmitting: boolean;
}
function usePressEnterFetch({ handleSubmit, isSubmitting }: IProps) {
const handlePressEnterFetch = (
e: React.KeyboardEvent<HTMLTextAreaElement>
) => {
if (e.key === "Enter" && !e.shiftKey && !isSubmitting) {
e.preventDefault();
handleSubmit(e);
}
};
return { handlePressEnterFetch };
}
export default usePressEnterFetch;
쓰는 곳
const { handlePressEnterFetch } = usePressEnterFetch({ handleSubmit });
<textarea
placeholder="내용을 입력해주세요..."
name="content"
value={inputValues.content}
className={styles.textarea}
onChange={onChange}
onKeyDown={handlePressEnterFetch}
/>