【问题标题】:Chain React setState callbacks链式反应 setState 回调
【发布时间】:2018-11-09 16:25:27
【问题描述】:

我需要按顺序加载三个不同的 json 文件并使用 fetch(原因是我正在使用 nextjs 导出,我需要动态读取这些文件,所以我在需要时获取它们并且它们的内容可以更改即使在导出之后)

第一个文件包含用于为第二个文件创建 url 的数据,依此类推,因此每次获取都需要一个 实际更新 状态才能获取,

ATM 我正在​​使用的解决方案,因为第二个和第三个文件分别依赖于第一个和第二个文件,正在获取第一个文件并使用 setState 设置一些状态,然后在 setState 回调中获取第二个文件并设置一些其他状态等等:

fetch(baseUrl).then(
            response => response.json()
        ).then(
            res => { 
                this.setState({
                    ...
                }, () => {
                    fetch(anotherUrl+dataFromUpdatedState).then(
                        response => response.json()
                    ).then(
                        res => { 
                            this.setState({
                                ...
                            }, () => {                                 
                                fetch(anotherUrl+dataFromUpdatedState).then(
                                    response => response.json()
                                ).then(
                                    res => {
                                        this.setState({

                                        })
                                    }
                                )
                            })
                        }
                    ).catch(
                        error => {
                            //error handling
                        }
                    )
                })
            }
        ).catch(
            error => {
                this.setState({ //an error occured, fallback to default
                    market: defaultMarket,
                    language: defaultLanguage,
                    questions: defaultQuestions
                })
                //this.setLanguage();
            }
        )

现在:我知道 setState 必须小心使用,因为它是异步的,但据我所知,回调函数是在状态更新后调用的,因此从这个角度来看,状态应该正确更新。这种解决方案是反模式、不好的做法还是出于某种原因应该避免?

代码确实有效,但我不确定这是否是这样做的方法。

【问题讨论】:

    标签: javascript reactjs callback setstate chain


    【解决方案1】:

    您不需要使用setState 回调并从状态中读取它,因为您可以直接从res 对象中读取数据。这样你就可以创建一个扁平的 Promise 链。

    示例

    fetch(baseUrl)
      .then(response => response.json())
      .then(res => {
        this.setState({
          // ...
        });
    
        return fetch(anotherUrl + dataFromRes);
      })
      .then(response => response.json())
      .then(res => {
        this.setState({
          // ...
        });
    
        return fetch(anotherUrl + dataFromRes);
      })
      .then(response => response.json())
      .then(res => {
        this.setState({
          // ...
        });
      })
      .catch(error => {
        this.setState({
          market: defaultMarket,
          language: defaultLanguage,
          questions: defaultQuestions
        });
      });
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2020-08-04
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多