首页 > 解决方案 > 方法 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>

标签: javascriptreactjs

解决方案


推荐阅读