import React, {useRef, useState} from "react";
const CommentForm = ({onAddComment}) => {
const [newCommentText, setNewCommentText] = useState('');
const textRef = useRef(null);
const handleAddComment = () => {
if (newCommentText.trim() !== '') {
onAddComment(newCommentText);
setNewCommentText('');
textRef.current.focus();
}
};
return (
<div>
<input
type="text"
ref={textRef}
value={newCommentText}
onChange={(e) => setNewCommentText(e.target.value)}
placeholder="输入你的评论"
/>
<button onClick={handleAddComment}>提交评论</button>
</div>
);
};
11-22
03-23
1331
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)