简体   繁体   English

反应加载状态不显示

[英]React loading state doesn't show

I'm fetching data from an API based on user input.我正在根据用户输入从 API 获取数据。 I'd like to display the text "Loading.." while the data is being fetch.我想在获取数据时显示文本“正在加载..”。

My problem is that this "Loading.." is never displayed no matter how slow my internet is.我的问题是无论我的互联网有多慢,这个“正在加载..”都不会显示。 Although I set isLoading to be true before fetching data.虽然我在获取数据之前将isLoading设置为 true 。

Additionally, I'm logging console.log("isLoading:", isLoading) in the body.此外,我正在主体中记录console.log("isLoading:", isLoading) Why does it get refreshed every i input something BEFORE hitting submit button.为什么每次我在点击提交按钮之前输入内容都会刷新。

import { useState } from "react";

// example user:  0x147412d494731cbb91dbb5d7019464a536de04dc

function App() {
  const [data, setData] = useState([]);
  const [enteredWallet, setEnteredWallet] = useState("");
  const [isLoading, setIsLoading] = useState(false);

  const walletChangeHandler = (event) => {
    setEnteredWallet(event.target.value);
  };

  const submittedHandler = (event) => {
    event.preventDefault();
    fetchNFTHandler(enteredWallet);
    console.log("enteredWallet:", enteredWallet);
  };

  function fetchNFTHandler(owner) {
    setIsLoading(true);
    fetch(
      `https://api.opensea.io/api/v1/assets?owner=${owner}&order_direction=desc&offset=0&limit=10`
    )
      .then((res) => {
        return res.json();
      })
      .then((data) => {
        const tranformedData = data.assets.map((element, index) => {
          return {
            title: element.name,
            id: index,
          };
        });
        setData(tranformedData);
      });
    setIsLoading(false);
  }

  return (
    <div className="App">
      <header className="App-header">
        <h3>Show me assets in this wallet</h3>
        <form onSubmit={submittedHandler}>
          <input
            placeholder="wallet address"
            value={enteredWallet}
            onChange={walletChangeHandler}
          />
          <button>Submit</button>
        </form>
        <div>
          {console.log("isLoading:", isLoading)}
          {!isLoading &&
            data.map((element) => <li key={element.id}>{element.title}</li>)}
          {isLoading && <p>Loading..</p>}
        </div>
      </header>
    </div>
  );
}

export default App;

Because you call setLoading(false) before your promise is resolved.因为你在你的承诺被解决之前调用了setLoading(false)

Do this instead:改为这样做:

function fetchNFTHandler(owner) {
    setIsLoading(true);
    fetch(
      `https://api.opensea.io/api/v1/assets?owner=${owner}&order_direction=desc&offset=0&limit=10`
    )
      .then((res) => {
        return res.json();
      })
      .then((data) => {
        const tranformedData = data.assets.map((element, index) => {
          return {
            title: element.name,
            id: index,
          };
        });
        setData(tranformedData);
        setIsLoading(false); // Move it here
      });
  }

Don't put your console.log-statement in the jsx btw, put it above the return statement.不要把你的console.log-statement放在jsx btw中,把它放在return语句的上面。

The setIsLoading(false); setIsLoading(false); is outside of the then(...) part.在 then(...) 部分之外。 This should do the trick:这应该可以解决问题:

  function fetchNFTHandler(owner) {
    setIsLoading(true);
    fetch(
      `https://api.opensea.io/api/v1/assets?owner=${owner}&order_direction=desc&offset=0&limit=10`
    )
      .then((res) => {
        return res.json();
      })
      .then((data) => {
        const tranformedData = data.assets.map((element, index) => {
          return {
            title: element.name,
            id: index,
          };
        });
        setData(tranformedData);
      })
      .finally(() => {
        setIsLoading(false);
      });
  }

Make sure to place it into a finally(...) as otherwise the loading state won't be turned off when an error occures.确保将其放入 finally(...) 否则加载状态不会在发生错误时关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM