
<input
type='checkbox'
onChange={() => handleToggle(continent._id)}
checked={checkedContinents.indexOf(continent._id) !== -1}
className='mr-1'
/>
onChange={() => handleToggle(continent._id)}
const handleToggle = (continentId) => {
const currentIndex = checkedContinents.indexOf(continentId);
const newChecked = [...checkedContinents];
if (currentIndex === -1) {
newChecked.push(continentId);
} else {
newChecked.splice(currentIndex, 1);
}
onFilters(newChecked);
};
currentIndex = checkedContinents.indexOf(continentId);
const newChecked = [...checkedContinents];
if (currentIndex === -1) newChecked.push(continentId);
else { newChecked.splice(currentIndex, 1); }
onFilters(newChecked);
onFilters 함수는 LandingPage 컴포넌트의 handleFilters 함수로 연결
<CheckBox
continents={continents}
checkedContinents={filters.continents}
onFilters={(filters) => handleFilters(filters, 'continents')}
/>
onFilters={(filters) => handleFilters(filters, 'continents')}
const handleFilters = (newfilteredData, category) => {
const newFilters = { ...filters };
newFilters[category] = newfilteredData;
showFilteredResults(newFilters);
setFilters(newFilters);
};
const newFilters = { ...filters };
newFilters[category] = newfilteredData;
showFilteredResults(newFilters);
setFilters(newFilters);
const showFilteredResults = (filters) => {
const body = {
skip: 0,
limit,
filters,
};
fetchProducts(body);
setSkip(0);
};
const body = { skip: 0, limit, filters };
상품 개수 제한과 필터 조건을 전달fetchProducts(body);
setSkip(0);