[英]How do I render a Company after 5 Products? | React js
I'm trying to make a ecommerce store and I want to render a <CompanyCard/>
component after every 2 <ProductCard/>
components.我正在尝试创建一个电子商务商店,我想在每 2 个
<ProductCard/>
组件之后呈现一个<CompanyCard/>
> 组件。 I've never done anything like this so I'm not sure if I'm doing the right approach我从来没有做过这样的事情所以我不确定我是否在做正确的方法
Here is a example of what I want:这是我想要的示例:
<ProductCard/>
<ProductCard/>
<CompanyCard/>
<ProductCard/>
<ProductCard/>
<CompanyCard/>
(and so on..)
but for some reason im getting a blank page with these 2 errors in the console但出于某种原因,我在控制台中得到了一个包含这两个错误的空白页面
The above error occurred in the <CompanyCard> component:
Uncaught TypeError: Cannot read properties of undefined (reading '_id')
This is my code (I'm also using React Redux)这是我的代码(我也在使用 React Redux)
function HomePage() {
let counter = 0;
const dispatch = useDispatch();
const productList = useSelector((state) => state.productList);
const { error, loading, products } = productList;
const companyList = useSelector((state) => state.companyList);
const { companies } = companyList;
useEffect(() => {
dispatch(listProducts(), listCompanies());
}, [dispatch]);
return (
<>
<Navbar />
<div className="homePage">
<div className="centerItemsContainer">
<div className="productsContainer">
{products.map((product) => {
counter++;
if (counter % 4 === 0) {
const companyIndex = counter / 4 - 1;
const company = companies[companyIndex];
return (
<>
<CompanyCard company={company} />
<ProductCard product={product} />
</>
);
} else {
return <ProductCard product={product} />;
}
})}
</div>
</div>
</div>
<Footer />
</>
);
}
export default HomePage;
First, it seems like you are simply selecting a company that does not exist.首先,您似乎只是在选择一家不存在的公司。 Log
companyIndex
and see what values you are using.登录
companyIndex
并查看您使用的是什么值。
Also, there is no need to manually keep track of counter
, it is the second argument from map
, so just write:此外,不需要手动跟踪
counter
,它是map
的第二个参数,所以只需写:
{products.map((product, counter) => {
I hope this helps if u want to render company on 4 th place pls change it to 4 same goes for what place u want我希望这会有所帮助,如果您想在第 4 位呈现公司,请将其更改为 4 同样适用于您想要的位置
const products = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
export const App = () => {
return (
<>
{products.map((product, index) => {
const counter = index + 1;
if (counter % 2 === 0) {
return (
<>
<p>Prod{index}</p>
<p>Company</p>
</>
);
}
return <p>Prod{index}</p>;
})}
</>
);
};
{products.map((item, index) => (
<>
<ProductCard />
{(!!index && !((index + 1) % 5) && <CompanyCard />)}
</>
)}
You are getting an error, because your iterator exceeds array length您收到错误消息,因为您的迭代器超出了数组长度
I would rather use a normal for loop for this:我宁愿为此使用普通的 for 循环:
const products1 = [...Array(10).keys()].map((e) => "p" + e); const companies1 = [...Array(10).keys()].map((e) => "c" + e); console.log(getList(products1, companies1)); const products2 = [...Array(10).keys()].map((e) => "p" + e); const companies2 = [...Array(1).keys()].map((e) => "c" + e); console.log(getList(products2, companies2)); function getList(products, companies) { const list = []; const min = Math.min(products.length, companies.length); for ( let i = 0, p = 0, c = 0; i < products.length + companies.length; i += 1 ) { // every fifth is a company, but only if there is a company left // or there are no more products if ((c < companies.length && i % 5 === 4) || p >= products.length) { list.push(companies[c++]); } else { list.push(products[p++]); } } return list; }
Final component:最后的组成部分:
function HomePage() {
let counter = 0;
const dispatch = useDispatch();
const productList = useSelector((state) => state.productList);
const { error, loading, products } = productList;
const companyList = useSelector((state) => state.companyList);
const { companies } = companyList;
function getList() {
const list = [];
const min = Math.min(products.length, companies.length);
for (
let i = 0, p = 0, c = 0;
i < products.length + companies.length;
i += 1
) {
if ((c < companies.length && i % 5 === 4) || p >= products.length) {
list.push(<CompanyCard company={companies[c++]} />);
} else {
list.push(<ProductCard product={products[p++]} />);
}
}
return list;
}
useEffect(() => {
dispatch(listProducts(), listCompanies());
}, [dispatch]);
return (
<>
<Navbar />
<div className="homePage">
<div className="centerItemsContainer">
<div className="productsContainer">{getList()}</div>
</div>
</div>
<Footer />
</>
);
}
export default HomePage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.