1

I'm trying to define a prop on a react component using a method. The method is working fine and I can return a correct result to the console. However when I inspect my component with react browser extension I can see no props are assigned to the component, nor can I use them within the component.

class NowPlaying extends Component {

state = {
    genres: []
};

componentDidMount() {
    // Fetch Genre Data
    axios.get(`${base_url}genre/movie/list?api_key=${api_key}`)
        .then(res => this.setState({ genres: res.data.genres }))
}

getGenreTitles = (genreIds, genres) => {
    genreIds.map(genreId => {
        const filteredGenres = genres.filter(genre => genre.id === genreId);
        const { name } = filteredGenres[0];
        return name;
    })
};

render() {
    return this.props.movies.map((movie) => (
       <MovieItem
           key={movie.id}
           movie={movie}
           genres={this.getGenreTitles(movie.genre_ids, this.state.genres)}
       />
    ));
}
}

Expectation is that the name is returned as a prop which can then be used in the movieitem component.

1 Answer 1

2

getGenreTitles doesn't return anything. (By contrast, note how the function you pass to map() has a return statement.) Either remove the curly braces from the function declaration to take advantage of the default return for arrow functions:

getGenreTitles = (genreIds, genres) =>
    genreIds.map(genreId => {
        const filteredGenres = genres.filter(genre => genre.id === genreId);
        const { name } = filteredGenres[0];
        return name;
    });

or add a return statement to the function body:

getGenreTitles = (genreIds, genres) => {
    return genreIds.map(genreId => {
        const filteredGenres = genres.filter(genre => genre.id === genreId);
        const { name } = filteredGenres[0];
        return name;
    });
}
Sign up to request clarification or add additional context in comments.

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.