[英]React.js flickering issue
I followed the online course to make a website and I realized that my website is blinking when I move to other pages.我按照在线课程制作了一个网站,当我移动到其他页面时,我意识到我的网站正在闪烁。 Are there any problems in my codes?我的代码有问题吗?
parent父母
import "./Product.css";
import Product from "./Product";
import { useLayoutEffect, useState } from "react";
import axios from "axios";
export default function Body() {
const [products, SetProducts] = useState([]);
useLayoutEffect(() => {
async function fetchProducts() {
const { data } = await axios.get("http://127.0.0.1:8000/api/products/");
SetProducts(data);
}
fetchProducts();
}, []);
return (
<div className="product">
{products.map((data) => (
<div key={data.id} className="product_child">
<Product product={data} />
</div>
))}
</div>
);
}
child孩子
import { Link } from "react-router-dom";
export default function Product({ product }) {
return (
<div className="product">
<div className="prodcut_child">
<Link
style={{ color: "inherit", textDecoration: "none" }}
to={`/product/${product.id}`}
>
<img src={product.img} alt="img" />
<p>{product.title}</p>
<h1 style={{ fontSize: "1.3rem" }}>${product.price}</h1>
</Link>
</div>
</div>
);
}
I used useEffect
and it was blinking, so I tried useLayoutEffect
instead but still, it happens.. (I used Link
tag tho)我使用useEffect
并且它在闪烁,所以我尝试useLayoutEffect
但仍然发生了......(我使用了Link
标签)
Is it because I use async await
to fetch api data?是因为我使用async await
来获取 api 数据吗?
Or I am using django rest_framework, is it a problem with low speed of django rest_framework??或者我使用的是 django rest_framework,是不是 django rest_framework 速度低的问题?
thank you..!谢谢你..!
I think its because of the async await request that your doing.我认为这是因为您所做的异步等待请求。
You should try and implement the loading feature in your app您应该尝试在您的应用中实现加载功能
something like this像这样的东西
export default function Body() {
const [products, SetProducts] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
async function fetchProducts() {
setLoading(true);
const { data } = await axios.get("http://127.0.0.1:8000/api/products/");
SetProducts(data);
}
fetchProducts();
setLoading(false);
}, []);
if (loading) return <div>loading...</div>
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.