0

Can someone help me understand why I am getting an error when I'm trying to apply values within my source prop?

I have the following block of code between render(){ and return(. It maps through an array of videos hosted locally with their corresponding paths.

render() {

const { loadVideo } = this.state;

let videoComp = [];
if(loadVideo){
  videoOptions.map((videoOptions) => {
    console.log('videoOptions console:',videoOptions.video)
    console.log('id console:',videoOptions.id)
    videoComp = (
      <Video 
        source={require(videoOptions.video)}   // this line produces an error
        key={videoOptions.id}
      />
    )
  })
}

return (

This code as-is returns the following error.

TransformError src/components/home/HomeScreen.js:
src/components/home/HomeScreen.js:
Invalid call at line 98:
require(videoOptions.video)

However, if I take out the line source={require(videoOptions.video)}, the error goes away and the two logs do spit out correct data.

enter image description here

Of course if I were to hard code source, everything is fine and dandy.

render() {

const { loadVideo } = this.state;

let videoComp = [];
if(loadVideo){
  videoOptions.map((videoOptions) => {
    console.log('videoOptions console:', `../../assets/videos/${videoOptions.video}`)
    console.log('id console:',videoOptions.id)
    videoComp = (
      <Video 
        source={require('../../assets/videos/087194062-dodgers-stadium_608x1080.mp4')}
        key={videoOptions.id}
      />
    )
  })
}

return (

EDIT: I removed a blurb asking why using a template literal inside require was also returning an error. That issue is a duplicate

Mix Master Mike
  • 1,079
  • 17
  • 26
  • what does your log of `videoOptions` show? – ic3b3rg Dec 28 '18 at 05:30
  • @certainperformance I updated my question by removing the portion about using incomplete stings inside `require` for RN. Please remove the duplicate tag. – Mix Master Mike Dec 28 '18 at 05:40
  • @ic3b3rg `videoOptions: {video: "../../assets/videos/087194062-dodgers-stadium_608x1080.mp4", id: 1}`. Only 1 object is showing up because I removed the rest for the time being until I get this resolved. – Mix Master Mike Dec 28 '18 at 05:44
  • It really does sound like the problem is that `videoOptions.video` cannot be determined to be *constant* - `require(anyvariable)` probably won't work for the same reason – CertainPerformance Dec 28 '18 at 05:50
  • @CertainPerformance so upon further digging based on everything said so far I have found that even my revised question is a [duplicate](https://stackoverflow.com/a/45418504/8378893). – Mix Master Mike Dec 28 '18 at 06:30

0 Answers0