import { useState } from 'react';
function useCommentInput() {
const [comment, setComment] = useState('');
const handleCommentChange = (event) => {
setComment(event.target.value);
};
const handleCommentSubmit = (event) => {
event.preventDefault();
// 여기서 댓글 등록 로직 추가
console.log('댓글 내용:', comment);
setComment('');
};
return [comment, handleCommentChange, handleCommentSubmit];
}
export default useCommentInput;
이 커스텀 훅 useCommentInput을 다음과 같이 사용
import useCommentInput from './useCommentInput';
function CommentSection() {
const [comment, handleCommentChange, handleCommentSubmit] = useCommentInput();
return (
<form onSubmit={handleCommentSubmit}>
<input
type="text"
value={comment}
onChange={handleCommentChange}
placeholder="댓글을 입력하세요"
/>
<button type="submit">등록</button>
</form>
);
}
export default CommentSection;