My ImageGallery component is not updating itself when my URL changes.
For example, when I click an item it routes me from '/movies/12345' to '/movies/9999', but the component itself is not updating to show the images associated with '/movies/9999'. Instead it shows the images associated with '/movies/12345'.
Parent Component:
class Parent extends Component {
componentDidMount() {
this.props.fetchImages(this.props.match.params.id);
}
render(){
..
<ImageGallery />
..
}
}
export default connect(mapStateToProps, { fetchImages })(Parent);
ImageGallery Component:
class ImageGallery extends Component {
render(){
return this.props.images.map(img => {
<img src={img.image_path}
})
}
}
And then I tried wrapping my connect function with withRouter and used componentWillReceiveProps. But this doesn't work it complains
Cannot read property id of Undefined
class Parent extends Component {
componentDidMount() {
this.props.fetchImages(this.props.match.params.id);
}
componentWillReceiveProps(nextProps) {
if (nextProps.match.params.id !== this.props.match.params.id) {
this.props.fetchImages(nextProps.params.id);
}
}
render(){
..
<ImageGallery />
..
}
}
export default withRouter(
connect(mapStateToProps, { fetchImages })(Parent)
);
App.js
<div>
<NavBar />
<Router history={history}>
<Route exact path='/' component={Landing} />
<Route exact path='/movies/:id' component={Parent} />
</Router>
</div>
How do I get the ImageGallery to display the correct images? Thanks.