2

I am trying to create multiple boxes with information and every box has an Icon. Right now I have hard-coded this as following:

<Box p={10} borderRadius='lg' bg={colorMode === 'light' ? 'gray.300' : 'gray.700'}>
    <Stack spacing={5}>
        <Box mt={5}>
            <IconContext.Provider value={{size: '10vh'}}>
                <div>
                    <IoLogoJavascript/>
                </div>
            </IconContext.Provider>
        </Box>
        <Box><Heading>JS & TS</Heading></Box>
    <Progress size="lg" value={75} hasStripe isAnimated/>
</Stack>
</Box>

Now, so that I can dynamically add more of these components, I want to implement a database. It's easy for the most part, I just don't know how I can implement it with the react-icons components? Should I use something else or would it be possible?

Joundill
  • 6,828
  • 12
  • 36
  • 50
Cryptorian420
  • 25
  • 2
  • 5

2 Answers2

14

You can create a custom component but you will need to import them all.

Here an example with fa icons:

import React from "react";
import "./styles.css";
import * as Icons from "react-icons/fa";

/* Your icon name from database data can now be passed as prop */
const DynamicFaIcon = ({ name }) => {
  const IconComponent = Icons[name];

  if (!IconComponent) { // Return a default one
    return <Icons.FaBeer />;
  }

  return <IconComponent />;
};

export default function App() {
  return (
    <div className="App">
      <DynamicFaIcon name="FaAngellist" />
      <DynamicFaIcon />
    </div>
  );
}
HichamELBSI
  • 1,712
  • 13
  • 19
0

How do you import your icons ? I would go the base64 way from DB to show it in frontend, cause i see no way to import them dynamically without building again React front...

CP3c0
  • 36
  • 4