I have the following route in my App component:
<Route path ='/:id' component = {VideoPage} />
When this route is visited, I want the VideoPage component to use the :id parameter to make an axios request to an API, and then I want to update VideoPage's state and render out the information that the API returns.
This is what I'm currently doing in the VideoPage component, which doesn't work:
export default class VideoPage extends Component {
constructor(props){
super()
this.state = {
id: props.match.params.id,
mainVideo: defaultVideo
};
}
componentDidUpdate(){
axios.get(getVideo(this.state.id))
.then(res => {
console.log(res)
this.setState({
mainVideo: res.data
})
})
.catch(err => console.log(err))
}
render() {
// render several components, passing them pieces of state from this component
}
The problem is that when I call this.setState
from within componentDidUpdate, I get an infinite loop.
So how would you go about doing this (making an API call using params from ReactRouter and the )?