简体   繁体   English

地图和使用 API 的 Reactjs 问题,浏览器没有显示任何内容

[英]Reactjs problem with map and using API, browser didnt show anything

I have a problem using API, i try to show the name on API contacts into web browser我在使用 API 时遇到问题,我尝试将 API 联系人上的名称显示到 Web 浏览器中

this is my code这是我的代码

NewMain新主

// import logo from './logo.svg';
// import './App.css';
// import List from './listapps/List.js'

class App extends Component {
  constructor(variable) {
    super(variable)
    this.state = {
      items: []
    }
  }

  componentDidMount() {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then(response => response.json)
      .then(data => this.setState({ items: data.items }))
  }

  render() {
    const { items } = this.state

    return (
      <div>
        <ul>{
          items && items.map((item, index) =>
            <li key={index} >{item.name}</li>
          )
        }
        </ul>
      </div>
    );
  }

}

export default App;

and i get blank, if i delete items&&如果我删除items&&我得到空白

i got this error我收到这个错误

Uncaught TypeError: Cannot read property 'map' of undefined
    at App.render (NewMain.js:25)
    at finishClassComponent (react-dom.development.js:18470)
    at updateClassComponent (react-dom.development.js:18423)
    at beginWork$1 (react-dom.development.js:20186)
    at HTMLUnknownElement.callCallback (react-dom.development.js:336)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:385)
    at invokeGuardedCallback (react-dom.development.js:440)
    at beginWork$$1 (react-dom.development.js:25780)
    at performUnitOfWork (react-dom.development.js:24695)
    at workLoopSync (react-dom.development.js:24671)
    at performSyncWorkOnRoot (react-dom.development.js:24270)
    at react-dom.development.js:12199
    at unstable_runWithPriority (scheduler.development.js:697)
    at runWithPriority$2 (react-dom.development.js:12149)
    at flushSyncCallbackQueueImpl (react-dom.development.js:12194)
    at flushSyncCallbackQueue (react-dom.development.js:12182)
    at scheduleUpdateOnFiber (react-dom.development.js:23709)
    at Object.enqueueSetState (react-dom.development.js:13994)
    at App.push../node_modules/react/cjs/react.development.js.Component.setState (react.development.js:325)
    at NewMain.js:15

what i want is to get the data and fill the data into list in <li> and then cjust reusable the list so i can look at it, but i can't get it right我想要的是获取数据并将数据填充到<li>列表中,然后重新使用该列表,以便我可以查看它,但我无法正确使用

so how can i solve it?那么我该如何解决呢?

In your componentDidMount method change response.json to response.json() .在您的componentDidMount方法中,将response.json更改为response.json() Also, the reason you are getting this error is because there is no items property in the data response object since it is just an array of objects.此外,您收到此错误的原因是data响应对象中没有items属性,因为它只是一个对象数组。 So, the right code should be:所以,正确的代码应该是:

fetch('https://jsonplaceholder.typicode.com/users')
  .then(response => response.json())
  .then(data => this.setState({ items: data }))

response.json is a function and needs to be called like this response.json() response.json是一个函数,需要像这样调用response.json()

following is correct code以下是正确的代码

 componentDidMount() {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then(response => response.json())
      .then(data =>console.log(data)|| this.setState({ items: data }))
 }

See it on codesandbox在代码和框上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM