简体   繁体   English

使用 ReactJS 从 API 获取数据

[英]Fetch data from API with ReactJS

I want to make simple CRUD functionality which should be the same in my web app, Android app and iOS app.我想制作简单的 CRUD 功能,在我的网络应用程序、Android 应用程序和 iOS 应用程序中应该是相同的。

Therefore, I have in node.js made an API from which I can retrieve a paginated list of all documents, a detail page of each document, and post/put requests to delete and update.因此,我在node.js了一个 API,我可以从中检索所有文档的分页列表、每个文档的详细信息页面以及发布/提交删除和更新请求。

Now I am able to check for administrator rights and other things at these routes.现在我可以在这些路由上检查管理员权限和其他内容。 Is this the correct approach?这是正确的方法吗?

Now I want to traverse this paginated list and present it with reactjs.现在我想遍历这个分页列表并用 reactjs 呈现它。

I guess it could be done with我想可以用

import React from 'react';
import ReactDOM from 'react-dom';

export default class ItemLister extends React.Component {
  constructor() {
    super();
    this.state = { items: [] };
    console.log('test');
  }

  componentDidMount() {
    fetch('/api/events/').then(result => {
      this.setState({items:result.json()});
    });
  }

  render() {
    return (
      <div>
        <div>Items:</div>
        { this.state.items.map(item => { return <div>{http://item.name}</div>}) }
      </div>
    );
  }
}

ReactDOM.render(<ItemLister/>, document.getElementById('hello-world'));

but nothing happens (it doesn't even print anything in the console window as i tried in the constructor).但没有任何反应(它甚至没有在我在构造函数中尝试时在控制台窗口中打印任何内容)。

I have this script in a file /components/ItemLister.jsx and I have imported react-15.2.1 and react-dom-15.2.1 .我在文件/components/ItemLister.jsx有这个脚本,并且我导入了react-15.2.1react-dom-15.2.1 I have installed babel-cli , babel-preset-es2015 , babel-preset-react and set我已经安装了babel-clibabel-preset-es2015babel-preset-react和 set

"presets": [
  "es2015",
  "react"
]

in my package.json .在我的package.json

Issue with {http://item.name} {http://item.name}

Put :把:

{ this.state.items.map(item =>  <div>http://{item.name}</div>) }

NOT :不是:

{ this.state.items.map(item => { return <div>{http://item.name}</div>}) }

Here with Sample code using SWAPI and showing results.这里有使用 SWAPI 的示例代码并显示结果。 Full code: https://react-vzbqum.stackblitz.io完整代码: https : //react-vzbqum.stackblitz.io

import React from 'react';
import ReactDOM from 'react-dom';

export default class ItemLister extends React.Component {
  constructor() {
    super();
    this.state = { items: [] };
    console.log('test');
  }

  componentDidMount() {
    fetch('https://swapi.co/api/planets').then(res => res.json()).then(res => {
      console.log(res.results);
      if (res.results && res.results.length > 0) {
        this.setState({items: res.results});
      }
    });
  }

  render() {
    return (
      <div>
        <div>Items:</div>
        { this.state.items.map(item => { return <div>{`http://${item.name}`}</div>}) }
      </div>
    );
  }
}

ReactDOM.render(<ItemLister/>, document.getElementById('hello-world'));

Your issue is at below line.你的问题在下面。

{ this.state.items.map(item => { return <div>{http://item.name}</div>}) }

As you using ES6 you no need to use return when returning on the same line.当您使用 ES6 时,在同一行返回时无需使用 return。 Replace the above code with below one...用下面的代码替换上面的代码...

{ this.state.items.map((item,index) => <div key={item.index}>http://{item.name}</div>)}

React recommends using key attribute with unique key value for better performance reconciliation when we insert dynamic nodes using map.当我们使用 map 插入动态节点时, React 建议使用具有唯一键值的属性以获得更好的性能协调。

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

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