React Native:setState延迟

Luc*_*iga 2 react-native

我正在尝试从获取响应中设置状态,但似乎需要一段时间来更新状态.

我学到的是fetch直到setState都很快.在那里,更新大约需要3秒钟.

fetch(ENDPOINT)
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({product : responseJson.product, related: responseJson.related, ready: true});
      })
      .catch((error) => {
        console.error(error);
    }).done();
Run Code Online (Sandbox Code Playgroud)

有小费吗?

谢谢

Har*_*uja 5

setState 是异步的.

如果您看到,请从反应本身的文档: -

setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value. There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains.

您可以在文档中阅读更多相关信息.

此外,你可以在这里查看类似的东西