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

122
Views
Icono de componente de estilo genérico

Estoy diseñando mi componente de reacción con componentes con estilo. Quiero un componente de ícono que se pueda usar en diferentes lugares simplemente cambiando el tamaño, los accesorios de color, etc. También quiero pasar los nombres de los íconos como accesorios para diferentes lugares. Logré cambiar el tamaño y el color, pero no sé cómo pasar el nombre del ícono según el requisito.

Aquí está mi componente de icono genérico:

 import React from "react"; import { ReactSVG } from "react-svg"; import styled, { css } from "styled-components"; import { FaUserTie } from 'react-icons/fa'; const StyledSVGIcon = styled(FaUserTie)` svg { fill: black; ${({ size }) => size && css` width: ${size}; height: ${size}; `} ${({ transform }) => transform && css` transform: ${transform}; `} path { ${({ color }) => color && css` fill: ${color}; `} } } `; const GenIcon = props => { return ( <StyledSVGIcon src={`/icons/${props.name}.svg`} color={props.color} size={props.size} transform={props.transform} /> ); }; export default GenIcon;

Y quiero usarlo así:

 <GenIcon name="FaUserNurse" color="red" size="16px" />

Pero el componente GenIcon no funciona. por favor ayúdame donde estoy haciendo mal. el ícono puede ser de cualquier tipo, como svg o cualquier otra biblioteca de íconos de reacción.

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

0

Prueba esto, estás cerca:

 import React from "react"; import { ReactSVG } from "react-svg"; import styled, { css } from "styled-components"; import { FaUserTie, FaDocker } from "react-icons/fa"; const IconStyler = styled.span` color: ${(props) => props.color}; & svg { ${(props) => props.small && css` width: 14px !important; height: 14px !important; `} ${(props) => props.med && css` width: 20px !important; height: 20px !important; `} ${(props) => props.large && css` width: 28px !important; height: 28px !important; `} } `; const Icon = ({ children, ...props }) => { return <IconStyler {...props}>{children}</IconStyler>; }; const GenIcon = () => { return ( <div> <h5>Any Icon</h5> <div> <Icon color="blue" small> <FaUserTie /> </Icon> </div> <div> <Icon color="orange" large> <FaDocker /> </Icon> </div> </div> ); }; export default GenIcon;

Aquí hay un enlace de sandbox: https://codesandbox.io/s/flamboyant-allen-60ho1?file=/src/GenIcon.js

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!