簡體   English   中英

"如何限制整個頁面在 React 中重新渲染?"

[英]How can I restrict whole page to re-render in React?

我有一個 js 文件,其中包含我使用useState<\/code>在頁面上呈現的數據。 我有一個按鈕Display<\/code> ,它使用fetch API<\/code>來獲取更多數據並顯示在現有數據下方。

我想在單擊Display button<\/code>直到數據到達時顯示Loading<\/code> 。

問題是使用Loading<\/code>重新呈現整個頁面。 我想按原樣顯示從 js 文件中獲取的數據,並在Display<\/code>按鈕下方顯示Loading<\/code> ,直到我從 API 獲取更多數據

 const [isLoading, setIsLoading] = useState(false);


  if (isLoading) {
    return <h2>Loading..</h2>;
  }
const Component = () => {
    const [isLoading, setIsLoading] = useState(false);
    const fetchData = () => {
         setIsLoading(true)
         fetch(someUrl)
            .then(data => {
                setData(data)
                setIsLoading(false)
         });
    }
    return (
    <> 
        {yourData.map((data) => <IdkMaybeItsSomeComponent data={data}>}

        { isLoading  
          ? <h2>Loading...</h2> 
          : <button onClick={fetchData}>Get More Data</button> }
    </>
    )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM