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

124
Views
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 answers
Answer question

0

You need to execute the icon component.

<IconContainer><Icons.ArrowRight /></IconContainer>}
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!