数据正在接收但无法显示

时间:2019-04-19 08:51:13

标签: javascript reactjs debugging

我正在尝试使用axios http请求显示一些数据,我正在测试如何以响应方式在客户端显示api数据。

https://codesandbox.io/s/4x291xp574

我的api通话正常运行,正如您在console中所见,我正在获取一些数据

我想使用data方法在div标签中显示map()。如果有人可以帮助我了解我在做什么错

2 个答案:

答案 0 :(得分:1)

这是您可行的代码。

import React from "react";
import ReactDOM from "react-dom";
import axios from "axios";

import "./styles.css";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      dogs: []
    };
  }

  componentDidMount() {
    axios("https://dog.ceo/api/breed/husky/images")
      .then(res => {
        console.log(res.data.message)
        this.setState({ dogs: res.data.message });
      })
      .catch(err => console.log(err));
  }

  render() {
    return (
      <div className="App">
        <h1>Hello CodeSandbox</h1>
        <h2>Start editing to see some magic happen!</h2>

        <div>
          {this.state.dogs.map(dog => (
            <div>
              <img src={dog}/>
            </div>
          ))}
        </div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


答案 1 :(得分:0)