Search code examples
reactjsreduxreact-reduxredux-promise-middleware

Access wanted data in API?


*apologies as I have posted a similar question earlier but deleted it as this is a better-structured question.

Hello! I want to target largeImageURL from the PixaBay API to render within the GridTile in my PhotoResults container. I've configured Redux into the application. When I console log this.props.photos after submitting a searchText, I receive an array with the length of 20. I know that 0: in 0: Array(20) means the first index position, but I don't know how that helps me to target largeImageURL. How do I target largeImageURL to render within the GridTile? Many thanks!

enter image description here

REDUCER

import * as actionTypes from '../actions/actions_index';

const photos = (state = [], action) => {
  switch(action.type) {
    case actionTypes.FETCH_PHOTOS:
      console.log('Action received', action);
      return [ action.payload.data.hits ];
    default:
      return state;
  }
}

export default photos;

PHOTO RESULTS CONTAINER

import React, { Component } from 'react';
import {GridList, GridTile } from 'material-ui/GridList';
import { connect } from 'react-redux';

class PhotoResults extends Component {
  render() {
    let photoList;
    if (this.props.photos) {
      photoList = (
        <GridList cols={3}>
          {this.props.photos.map(photo => (
            <GridTile title={photo.tags} key={photo.id}>
            <img src={photo.largeImageURL} alt="" />
            </GridTile>
          ))}
        </GridList>
      )
    } else {
      photoList = null;
    }
    console.log(this.props.photos);
    return (
      <div>
        {photoList}
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return { photos: state.photos };
}

export default connect(mapStateToProps, null)(PhotoResults);

Solution

  • Try this. Initially this.props.photos will be undefined so you need to do conditional check before doing .map

      return(){
          return(
               <div>
                  <GridList cols={3}>
                      {this.props.photos && Array.isArray(this.props.photos[0]) && this.props.photos[0].length > 0 && this.props.photos[0].map(photo => (
                         <GridTile title={photo.tags} key={photo.id}>
                             <img src={photo.largeImageURL} alt="" />
                        </GridTile>
                       ))}
        </GridList>
              </div>
          )
      }
    

    This will also work but I don’t recommend if else in render

        render() {
           let photoList;
           if (this.props.photos && Array.isArray(this.props.photos[0]) && this.props.photos[0].length > 0)
               photoList = (
                   <GridList cols={3}>
                        {this.props.photos[0].map(photo => (
                           <GridTile title={photo.tags} key={photo.id}>
                             <img src={photo.largeImageURL} alt="" />
                           </GridTile>
                        ))}
                   </GridList>
                  )
                 } else {
                 photoList = null;
                }
                console.log(this.props.photos);
               return (
               <div>
                  {photoList}
               </div>
              );
            }