1

This will give an error but hopefully it shows pretty much but I want to do. I want to have a bunch of icons in my component (later on they will be links) but I'm wondering if there's a way to bring them in dynamically rather than hard-coding them.

Data

const icons = [
    {
        title: 'BsGithub',
        path: 'bs'
    }, {
        title: 'BsStackOverflow',
        path: 'bs'
    }, {
        title: 'BsLinkedin',
        path: 'bs'
    },
]

export default icons

Component File

import icons from '../../data/icons'
const MyComponent = () => {
    const getIcon = ({ title, path }) => {
        import { title } from `react-icons/${path}`

        return(
            <h1>
                <{title} />
            </h1>
        )
    }
    return(
        <div>
            {icons.map(icon => getIcon(icon))}
        </div>
    )
}
easymoney
  • 85
  • 5

1 Answers1

0

Here was my final solution, omitting all styling-related code.

Data

import { BsGithub, BsStackOverflow, BsLinkedin } from 'react-icons/bs'

const icons = [
    {
        picture: <BsGithub />,
        link: '[insert link here]'
    }, {
        picture: <BsStackOverflow />,
        link: '[insert link here]'
    }, {
        picture: <BsLinkedin />,
        link: '[insert link here]'
    }
]

export default icons

Component File

import icons from '../../data/icons'

const MyComponent = () => {
    const getIcon = ({ picture, link }) => {
        return(
            <a href={link} target='_blank'>{picture}</a>
        )
    }
    return(
        <div>
            {icons.map(icon => getIcon(icon))}
        </div>
    )
}

export default MyComponent
easymoney
  • 85
  • 5