javascript - 方法 fetch post 慢慢地将我的信息添加到状态
问题描述
当我获取帖子时,应用程序很慢。我进入反应开发者工具,看看添加后你必须等待大约 1 秒钟,然后才能将适当的 ID 添加到 PointId 状态。这可能是由什么引起的?填写表格并点击确认按钮后:
appendElement = (Name, Description, X, Y, RouteId) => {
const newItem = {
"Name": Name,
"Y": Y,
"X": X,
"RouteId": 1,
"Description": Description
}
fetch("http://api/Points", {
method: "post",
headers: {
"Content-Type": "application/json",
"Authorization": `bearer ${sessionStorage.getItem("access_token")}`
},
body: JSON.stringify(newItem)
})
.then(res => res.json())
.then(res => {
this.setState({
PointId: [...this.state.PointId, res.Id]
})
alert("dodales")
})
this.setState({
elements: ([...this.state.elements, { Name, Description, X, Y, RouteId }]),
})
渲染方法:
<button className="btn" onClick={() => this.appendElement(this.state.Name, this.state.Description, this.state.Y, this.state.X, this.state.RouteId)}>Dodaj punkt</button>
解决方案
推荐阅读
- javascript - 递归调用按什么顺序堆叠
- php - Twig 模板转储函数的输出没有样式
- string - 通过在(包括)变量中的第一个字母之后匹配字符串来将产品名称与字符串隔离
- r - map_df 函数可以工作,但是当在 Plumber api 中它失败了
- node.js - 从硒元素中获取字符串
- c++ - 私有地图值的基于范围的 for 循环
- php - Codeigniter PHPwork
- python-3.x - 在单元测试中获取 Celery 重试方法的参数
- ios - 如何创建 if 语句来确定在 tableView 中使用什么数组?
- scala - Sparkstreaming + Kafka 到 hdfs