What is wrong with the string interpolation here

here in react native , I am getting error while attempting this

   <Image source={require(`../assets/${expand[0] ? 'expand':'expandDown'}.png`)} />

expand[0] is true/false
error name simply syntax error

You can’t use a template literal in a require statement, but I think something like this would work:

    ? <Image source={require('../assets/expand.png')} />
    : <Image source={require('../assets/expandDown.png')} />}
1 Like

Thanks. this works

But i remember I did something similar in my last project and it worked there. Maybe it was something else, thanks again

I’d be interested if you find anything out, I’m just getting into React Native. From this link, it seems that dynamically requiring assets isn’t possible(?):

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.