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

165
Views
Pass Custom Relative Image source to Image Tag React Native

I am trying to create a custom component in React Native which contains an Image tag with a custom source to be passed to it. The problem now is, I am getting an error that says Invalid call at line 17: require(imageSrc).

This is my GoalButton component code below:

const GoalButton = ({
  buttonStyle,
  onPress,
  text,
  imageSrc,
  imageStyle,
  textStyle,
}) => {
  return (
    <TouchableOpacity onPress={onPress}>
      <View style={buttonStyle}>
        <Image style={imageStyle} source={require(imageSrc)} />
        // Above line is Line 17
        <Text style={textStyle}>{text}</Text>
      </View>
    </TouchableOpacity>
  );
};

GoalButton.defaultProps = {
  buttonStyle: styles.buttonStyle,
  onPress: () => null,
  imageStyle: styles.imageStyle,
  textStyle: styles.textStyle,
};

export default GoalButton;

And I call it from outside like this:

<GoalButton text={'Meal Plan'} imageSrc={'../assets/images/plate.png'} />;

My question is, is there a way I can pass a relative image URL to the component and access it without any error like I was using it directly on the Image component.

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

0

it is not possible, you can looked this,

https://stackoverflow.com/questions/30854232/react-native-image-require-module-using-dynamic-names#:~:text=The%20only%20allowed%20way%20to%20refer%20to%20an%20image%20in%20the%20bundle%20is%20to%20literally%20write%20require(%27image!name-of-the-asset%27)%20in%20the%20source.

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!