코드 전문은 아래와 같습니다.
import React,{useEffect,useState} from 'react'
import { FaCode } from "react-icons/fa";
import axios from "axios"
import {Icon, Col, Card,Row } from 'antd';
import Meta from 'antd/lib/card/Meta'
import ImageSlider from '../Utils/ImageSlider'
import CheckBox from "./Sections/CheckBox"
import continents from './Sections/Datas'
function LandingPage() {
const [products, setproducts] = useState([]);
const [Skip, setSkip] = useState(0);
const [Limit, setLimit] = useState(4);
const [PostSize, setPostSize] = useState(4);
const [Filters, setFilters] = useState({
continents : [],
price :[]
});
useEffect(() => {
let body = {
skip : Skip,
limit : Limit
}
getProducts(body);
}, [])
const getProducts = (body)=>{
axios.post('/api/product/products', body)
.then(response=>{
if(response.data.success){
if(body.loadMore){
setproducts([...products,...response.data.productInfo]);
}else{
setproducts(response.data.productInfo);
}
console.log("response.data.PostSize" + response.data.postSize);
setPostSize(response.data.postSize);
}else{
alert('상품정보들을 가져오지 못했습니다. ')
}
})
}
const renderCards = products.map((product,index)=>{
return <Col lg={6} md={8} xs={24} key={index} >
<Card
key={index}
cover={<ImageSlider images={product.images}/>}
>
<Meta
title={product.title}
description={`$${product.price}`}
/>
</Card>
</Col>
})
const loadMoreHandler = (e)=>{
let skip = Skip + Limit;
let body = {
skip : skip,
limit : Limit,
loadMore:true
}
getProducts(body);
setSkip(skip);
}
const showFilterResults = (filters)=>{
let skip = Skip + Limit;
let body = {
// db에서 처음부터 가져와야 하니까
skip : 0,
limit : Limit,
filters : filters
}
getProducts(body);
setSkip(0);
}
const handleFilters= (filters,category)=>{
const newFilters = {...Filters}
newFilters[category] = filters ;
showFilterResults(newFilters);
}
return (
<div style={{width:'75%', margin:'3rem auto'}}>
<div style={{textAlign:'center'}}>
<h2>Let's travel around <Icon type="rocket"/></h2>
</div>
{/* filter */}
{/* Check Box */}
<CheckBox list={continents}
handleFilters ={filters=>handleFilters(filters,"continents")}
/>
{/* Radio Box */}
{/* search */}
{/* Cards */}
<Row gutter={[16,16]}>
{renderCards}
</Row>
<br/>
{PostSize>=Limit &&
<div style={{display:'flex', margin:'3rem auto', justifyContent:'center'}}>
<button onClick={loadMoreHandler}>더보기</button>
</div>
}
</div>
)
}
export default LandingPage
좋은 강의 감사드립니다.
궁금했는데 마침 있네요!
자식 컴퍼넌트 정보를 부모컴퍼넌트에서 받아오는부분이 항상 헷갈리는거같아요
설명해주신부분 읽어보면 음 그렇구나 해도
막상 코드부분을보면 다시 또 헷갈리는느낌 ㅠ