How can I set a default video in my player with componentDidMount?

Viewed 40

I am making a video player in React that fetches API from yt and you can search videos and it shows them. Everything works fine but when the app first loads the search is empty, so I want it to have a video there as default when it loads, using componentDidMount. but I cannot figure out how to write my componentDidMount so it fetches the url and plays the video when it first renders.

here is my whole App component.

class App extends React.Component {
state = {
    videos: [],
    selectedVideo: null
}

componentDidMount() {
    //fetch("https://www.youtube.com/watch?v=EaswWiwMVs8")
       // .then((response) => response.json())
       

}


handleSubmit = async (termFromSearchBar) => {
    const response = await youtube.get('/search', {
        params: {
            q: termFromSearchBar
        }
    })

    this.setState({
        videos: response.data.items
    })
    console.log("this is resp", response);
};
handleVideoSelect = (video) => {
    this.setState({ selectedVideo: video })
}

render() {
    return (
        <div className='ui container' style={{ marginTop: '1em' }}>
            <SearchBar handleFormSubmit={this.handleSubmit} />
            <div className='ui grid'>
                <div className="ui row">
                    <div className="eleven wide column">
                        <VideoDetail video={this.state.selectedVideo} />
                    </div>
                    <div className="five wide column">
                        <VideoList handleVideoSelect={this.handleVideoSelect} videos={this.state.videos} />
                    </div>
                </div>
            </div>
        </div>
    )
 }
 }

 export default App;
0 Answers
Related