Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
How to require and export icons so that I can dynamically import using a string value?

I have a string that is the name of the icon I want to import/require and I'm trying to import/require the SVG file that I want to use in React Native dynamically. My initial idea was this:

const icon = require(`@src/assets/icons/${iconName}`)

However, I realized that this would not work and that I need to have all my icon options imported. This is why I created an Icons.ts file from which I'm trying to require and then export all my icons like this:

const Icons = {
    ArrowRight: import('@src/assets/icons/ArrowRight.svg'),
    ArrowLeft: import('@src/assets/icons/ArrowLeft.svg'),
};

export default Icons;

And then I attempted to use them in my component like by hardcoding Icons.ArrowRight:

{leftIcon && <IconContainer>{Icons.ArrowRight}</IconContainer>}

Which unfortunately didn't work and I got the following error:

Objects are not valid as a React child.

If I import an icon straight in my component like this:

import Close from '@src/assets/icons/close.svg';

I can then use it like this:

<Close />
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to execute the icon component.

<IconContainer><Icons.ArrowRight /></IconContainer>}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda