0

I'm trying to capture wepb url from an axios response and pass it to an image component.

I want to loop through data and show every data[all].images.original.webp

I've tried .map() with no success

I think some of my problems involve waiting on the response to finish, and UserItem is probably all wrong

Here is the console.log I get during troubleshooting.

enter image description here

App

import React, { Component } from "react";
import axios from "axios";
import Users from "./components/Users";

class App extends Component {
  state = {
    users: [] /* Set users inital state to null */,
    loading: false
  };

  async componentDidMount() {
    this.setState({ loading: true });

    const res = await axios.get(
      "http://api.giphy.com/v1/stickers/search?q=monster&api_key=sIycZNSdH7EiFZYhtXEYRLbCcVmUxm1O"
    );

    /* Trigger re-render. The users data will now be present in 
       component state and accessible for use/rendering */
    this.setState({ users: res.data, loading: false });
  }

  render() {
    return (
      <div className="App">
          <Users loading={this.state.loading} users={this.state.users} />
          {console.log(this.state.users)}
        </div>
      </div>
    );
  }
}

export default App;

Users Component

import React, { Component } from "react";
import UserItem from "./UserItem";

export default class Users extends Component {
  render() {
    return (
      <div className="ui relaxed three column grid">
        {this.props.users.map(data => (
          <UserItem key={data.id} gif={data.images.original.webp} />
        ))}
      </div>
    );
  }
}

UserItem

import React from "react";
import PropTypes from "prop-types";

const UserItem = ({ user: { gif } }) => {
  return (
    <div className="column">
      <img src={gif} className="ui image" />
    </div>
  );
};

UserItem.propTypes = {
  user: PropTypes.object.isRequired
};

export default UserItem;

Error Message

enter image description here

2 Answers 2

1

So it took me a while to read up on the giphy api, but it turns out you might possibly be using the wrong protocol, http instead of https, so the axios call was actually throwing an error and that was getting saved in state since your code doesn't handle it, i.e. state.users wasn't an array to map over.

axios.get("https://api.giphy.com/v1/stickers/search?q=monster&api_key=sIycZNSdH7EiFZYhtXEYRLbCcVmUxm1O")

The response data is also response.data.data, and your UserItem component just receives gif as a prop, not the user object. I've coded up a working sandbox.

Edit Send Array from API Response to Components

Sign up to request clarification or add additional context in comments.

Comments

0

Render the output in a conditional expression so that it does not try to render the .map() before the array of data is available. Likely that this.props.users is undefined the first time the component tries to render and so you get a fatal TypeError.

{this.props.users && this.props.users.map(data => (
    <UserItem key={data.id} gif={data.images.original.webp} />
))}

The && expression acts as a boolean conditional when used like this. Now the first time the component tries to render and this.props.users is undefined it will not try and run the .map(), when the props update and the array is available it will render.

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.