javascript - 正在接收数据但无法显示

标签 javascript reactjs debugging

我正在尝试使用axios http请求来显示一些数据,我正在测试如何使用react将api数据显示到客户端。

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

我的 api 调用正常工作,您可以在控制台中看到我正在获取一些数据

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

最佳答案

这是您的可行代码。

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);


关于javascript - 正在接收数据但无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55759134/

相关文章:

javascript - 导入模块 NodeJS 中的所有导出

javascript - 使用 JavaScript 清除 div 元素

javascript - React.js 必须返回有效的 React 元素(或 null)

c++ - 无法引用非静态字段 "x"

ruby - BasicObject 中的 binding.pry

javascript - native 日期输入忽略 CSS

javascript - html5 拖放 - 不需要的 javascript 循环

javascript - jquery-InputMast 覆盖输入字段上的现有事件

javascript - 尝试根据 FieldArray React JS 内部的条件渲染字段

css - 有哪些可用的 CSS 调试工具?