1

I want to export array from one file into another file in react.js. In this array, I randomly select 5 videos from video collection and store these array and export this array to the task1 file and broadcast this url array in the video. See my code. This is Array.js.

function Array (){
    const colUrl =[]
    const url =[
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
        ]
     for (var i=0;i<5;i++){
         var rand = url[Math.floor(Math.random()*10)];
         colUrl.push(rand);
      }

      return colUrl;

}

export default Array;

This is the file I want to import and use in the video.

import React, { Component } from 'react';
import ReactPlayer from 'react-player';
import Array from './Array';
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

function Task1 () {
    return (
      <div>
          <div className="head">
          <h1>Experiment</h1>
          <h3>Task 1</h3>
          <p>Instruction: This experiment includes 3 tasks. </p>
          <p>In Task 1, you will see 10 videos. </p>
      </div>
     <div className="video">
           <ReactPlayer  url={Array[0]} playing/>
        </div>                  
        <div> 
            <button className="task1-next">Next Video</button>
            <Link to="/task2"><button>Go to Task2</button></Link>

        </div>      

</div>

    )
  }


export default Task1;

2 Answers 2

4

Your Array is a function. What you exported is a function. But what you actually want is an array of 5 random items. So you should execute the function and export the value.

Like this:

function Array (){
    const colUrl =[]
    const url =[
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
         "https://www.youtube.com/watch?v=d46Azg3Pm4c",
        ]
     for (var i=0;i<5;i++){
         var rand = url[Math.floor(Math.random()*10)];
         colUrl.push(rand);
      }

      return colUrl;

}

export default Array(); // <-- Execute the Array function
Sign up to request clarification or add additional context in comments.

1 Comment

@WhiteWhale I'll appreciate if you could mark my answer as correct. :P
0

You must use the exported function like this.

<ReactPlayer url={Array()[0]} playing/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.