Ive been wondering what the best way to approach this is, if I should go through the elements of my array with a For loop or Map.
This is my array:
templates = [ { title: "Grocery List", description: "Description of what this things does so the reader can have info of", imgURL: "https://res.cloudinary.com/deruwllkv/image/upload/v1625753993/groceries.png", id: 0, }, { title: "Shopping Space", description: "blablabalblabla", imgURL: "https://res.cloudinary.com/deruwllkv/image/upload/v1625753766/shopping.png", id: 1, }, { title: "Travel Planning", description: "blablabalblabla", imgURL: "https://res.cloudinary.com/deruwllkv/image/upload/v1625753885/travel.png", id: 2, }, { title: "Travel", description: "blablabalblabla", imgURL: "https://res.cloudinary.com/deruwllkv/image/upload/v1625753993/groceries.png", id: 3, }, ]; Which is processed by my TemplateList component that maps each element. I would like to convert this to a For loop but I dont know how I should approach it. Also, Is it better to have a For loop in this case?
TemplateList:
export type Template = { title: string; description: string; imgURL: string; id?: number; }; type Props = { templates: Template[]; }; const TemplateList = ({ templates }: Props) => { return ( <div className={styles.scrollContainer}> {templates.map((item) => ( <TemplateCard title={item.title} description={item.description} img={item.imgURL} classNameToAdd={styles.cardContainer} key={item.id} /> ))} </div> ); }; export default TemplateList; This is my TemplateCard component:
type Props = { title: string; description: string; img: string; classNameToAdd?: string; classNameOnSelected?: string; }; const TemplateCard = ({ title, description, img, classNameToAdd, classNameOnSelected }: Props) => { const { aspectRatio, vmin } = useWindowResponsiveValues(); let className = `${styles.card} ${classNameToAdd}`; const [selected, setSelected] = useState(false); const handleClick = () => { setSelected(!selected); }; if (selected) { className += `${styles.card} ${classNameToAdd} ${classNameOnSelected}`; } return ( <div style={card} className={className} onClick={handleClick}> <img style={imageSize} src={img}></img> <div style={cardTitle}> {title} {selected ? <BlueCheckIcon style={blueCheck} className={styles.blueCheck} /> : null} </div> <div style={descriptionCard}>{description}</div> </div> ); }; TemplateCard.defaultProps = { classNameOnSelected: styles.selected, }; export default TemplateCard;