如何在React with useState中提交后清除字段



我正在开发Mernstack应用程序,除了提交后未清除的输入字段外,我的应用程序中的所有内容都运行良好。

我的组件

const [comments, setCommentData] = useState([]);

const onSubmit = useCallback(
(e) => {
e && e.preventDefault();

axios
.post(
"http://localhost:9000/events/" +
props.match.params.id +
"/eventcomment",
{ name: name, description: eventDescription }
)
.then(function (response) {
console.log("response", response.data.eventcomments);
onPageLoad();
})
.catch(function (error) {
console.log(error);
});
},
[props.match.params.id, name, eventDescription]
);

我试过这样做,但没有成功。

setCommentData("");

初始状态为[],因此应将其恢复为相同的初始内容:

setCommentData([]);

最新更新