Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why is axios returning the previous images from flickr even if the tags are logged correctly.

import React, { Component } from 'react'; import ImageList from './FetchData'; import axios from 'axios'; import apiKey from '../config'; import Image from './Image';

class PhotoContainer extends Component { constructor(props) { super(props); this.state = { images: [], loading: true }; }

componentDidMount = () => {
    this.performSearch(this.props.tag);
};

shouldComponentUpdate = nextProps => {
    if (this.props.tag === nextProps.tag) {
        return false;
    } else {
        console.log(nextProps.tag);
        this.performSearch(nextProps.tag);
        return true;
    }
};

performSearch = tag => {
    console.log('tag' + tag);
    axios
        .get(
            `https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&tags=${tag}&per_page=8&format=json&nojsoncallback=1`
        )
        .then(response => {
            //  console.log(response.data);
            this.setState({
                images: response.data.photos.photo,
                loading: false
            });
        })
        .catch(error => {
            console.log('Error fetching and parsing data', error);
        });
    return;
};

render() {
    return (
        <div className="photo-container">
            <h2>{this.props.tag}</h2>
            <ul>
                {this.state.loading ? (
                    <p>loading</p>
                ) : (
                    this.state.images.map(image => (
                        <Image
                            url={`https://farm${image.farm}.staticflickr.com/${
                                image.server
                            }/${image.id}_${image.secret}.jpg`}
                            title={image.title}
                            key={image.id}
                        />
                    ))
                )}

                <li className="not-found">
                    <h3>No Results Found</h3>
                    <p>You search did not return any results. Please try again.</p>
                </li>
            </ul>
        </div>
    );
}

}

export default PhotoContainer;