Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
React - List icons with different param

I'm trying to use the map function but I can't get it right. I have a side-bar which I want to show some icons. Here is an example without the map.

    const SideBar = () => {

    return (
        <div className="fixed top-0 left-0 h-screen w-20 m-0 flex flex-col bg-gray-100 text-white shadow-lg">
            <SideBarIcon icon={<FaFire size="30" />} />
            <SideBarIcon icon={<FaPoo size="30" />} />
        </div>
    );
};

const SideBarIcon = ({ icon, text = "tooltip 💡"}) => (
    <div className="sidebar-icon group">
        {icon}
        <span class="sidebar-tooltip group-hover:scale-100">{text}</span>
    </div>
);

Here is an example with the map function

const SideBar = () => {
    const icons = [FaFire, FaPoo];

    return (
        <div className="fixed top-0 left-0 h-screen w-20 m-0 flex flex-col bg-gray-100 text-white shadow-lg">
            {icons.map(function(icon) {
                return <SideBarIcon icon={<icon size="30"/>}/>
            })}
        </div>
    );
};

const SideBarIcon = ({ icon, text = "tooltip 💡"}) => (
    <div className="sidebar-icon group">
        {icon}
        <span class="sidebar-tooltip group-hover:scale-100">{text}</span>
    </div>
);

Can you tell me what I'm doing wrong? Thank you for your time!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

{icons.map(function (Icon) {
    return <SideBarIcon icon={<Icon size="30"/>}/>
})}

Components start with capital letters, just change icon to Icon. Please do not forget to use key prop for your mapped items.

https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized

about 4 years ago · Juan Pablo Isaza Report

0

By simply putting icon inside the tags, it thinks you're rendering an HTML element called icon, therefore it's not rendering the mapped item. It also wouldn't work if you set it as <{icon}/>, because it would be trying to render an empty element.

Luckily, there's an easy fix -- Just capitalize Icon, and React will render it as a JSX Component.

{icons.map(function(Icon) {
    return <SideBarIcon icon={<Icon size="30"/>}/>
})}
about 4 years ago · Juan Pablo Isaza Report

0

const SideBar = () => {
const icons = [<FaFire size="30" />, <FaPoo size="30" />];

return (
    <div className="fixed top-0 left-0 h-screen w-20 m-0 flex flex-col bg-gray-100 text-white shadow-lg">
        {icons.map(function(icon) {
            return <SideBarIcon icon={icon}/>
        })}
    </div>
);

};

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!