[英]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.