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?
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>
);
}