【发布时间】:2020-05-21 22:56:55
【问题描述】:
- 提交后如何重置表单?现在提交表单后,详细信息仍然显示在表单中。目前表单只是提交到服务器,没有任何与数据库的连接。
- 另外,是否可以
console.log()在服务器端提交的详细信息?我试过console.log("Serverside printing:"+req.newstitle);,但我越来越不确定。
server.js:
app.post('/service/news', (req, res) => {
console.log("Serverside printing:"+req.newstitle);
res.json({ express: "news" })
});
News.js:
const [newstitle, setNewsTitle]=useState([]);
const [newsDetails, setNewsDetails]=useState([]);
const [isSent, setIsSent] = useState(false);
const successfullMessage = <p>Details submittted successfully !</p>
const form = <form>...</form>
const handleSubmit = e => {
e.preventDefault()
fetch('http://localhost:3000/service/news', {
method: 'POST',
body: JSON.stringify({ newstitle, newsDetails })
}).then(() => setIsSent(true))
}
return(
<div className="container">
<div className="layout">
<span className="col col-main">
<h3>What is so special about Us ?</h3>
<span className="specialAboutus_data_1">
<form onSubmit={handleSubmit} className="myForm">
<div className="loginfillContentDiv formElement">
<label>
<input name="newstitle" className="inputRequest formContentElement" type="text" placeholder="Title"
onChange={e => setNewsTitle(e.target.value)}/>
</label>
<label>
<textarea name="newsdetails" className="inputRequest formContentElement" type="textarea" placeholder="News details"
onChange={e => setNewsDetails(e.target.value)}/>
</label>
</div>
<div className="loginsubmitButtonDiv formElement">
<button type="submit" className="submitButton">Save</button>
</div>
</form>
</span>
</span>
<span className="col col-complementary">
<h3>Are you keen to join us ? </h3>
<span className="joinAboutus_data_1">
{isSent ? successfullMessage : form }
</span>
</span>
</div>
</div>
)
【问题讨论】:
标签: reactjs express axios react-hooks