Saving react icons in mysql database

I have an application, with a comment section, where I want to make my own Icon list, so you can add React Icons to comments, but I want to know if it's possible to save them in a MySQL database, along with plain text?

I have a row called contents, and for simplicity and want to able to save my icons alongside that. If not possible I was wondering if there was something you could do to maybe encode it from the server side to the database.

Is there any similar alternative that could be used?

Upvotes: 1

Views: 612

Answers (1)

RandomGuy01
RandomGuy01

Reputation: 25

You can import the libraries being used and index them from the string in the DB as seen below:

import * as FaIcons from "react-icons/fa";
import * as MaDesign from "react-icons/md";

const SpecificIcon = () => {
  if (amenity.amenity.library === "FaIcons") {
    const IconComponent = FaIcons[amenity.amenity.icon as keyof typeof FaIcons]
    return <IconComponent />
}
if (amenity.amenity.library === "MaDesign") { 
  const IconComponent = MaDesign[amenity.amenity.icon as keyof typeof MaDesign]
  return <IconComponent />
}
// Default case if nothing matched
return <FaIcons.FaBeer />
}

return (
  <SpecificIcon />
)

Upvotes: 1

Related Questions