[英]Best way to access API from React App?
What's the best way for me to access an API using a React app? 使用React应用程序访问API的最佳方法是什么? The API is currently developed in Golang using kami & mgo for the POST/GET/DELETE requests.
该API当前是在Golang中使用kami和mgo开发的,用于POST / GET / DELETE请求。
I want to be able to make a GET request to the following URL: 我希望能够对以下URL发出GET请求:
http://user:password@localhost:8000/api/v1/systems http:// user:password @ localhost:8000 / api / v1 / systems
on my React app and store the result in a state attribute: 在我的React应用程序上并将结果存储在state属性中:
this.state = {
data: //store the data here
}
I also want to load this data whenever the page is loaded so maybe I should use the componentDidMount() function to handle this? 每当页面加载时,我也想加载此数据,所以也许我应该使用componentDidMount()函数来处理此问题?
I've never used API calls on React, so I was wondering if anyone here could tell me a good way to this? 我从来没有在React上使用过API调用,所以我想知道这里是否有人可以告诉我解决这个问题的好方法?
EDIT 编辑
I'm using React 15.3.2. 我正在使用React 15.3.2。
EDIT #2 编辑#2
I've taken a look at fetch to handle the requests, but I'm still unsure how to use it in my situation. 我已经看过了fetch来处理请求,但是我仍然不确定如何在我的情况下使用它。 I've got the react app running on localhost:3000 and the api running on localhost:8000, /api/v1/systems will return a JSON with the following format:
我有在本地主机:3000上运行的react应用程序和在本地主机:8000上运行的api,/ api / v1 / systems将返回以下格式的JSON:
{ systems : [ //list of objects ] }
I've tried the following inside my componentDidMount(): 我在我的componentDidMount()中尝试了以下方法:
fetch(myRequest)
.then(result => {
console.log(result);
//this.setState({ data: result.json() });
});
Not too sure what myRequest should be (been trying with a simple string of the URL: ' http://localhost:8000/api/v1/systems ') and I'm also not sure if the ports where the apps are running could make a conflict or something. 不太清楚myRequest应该是什么(尝试使用URL的简单字符串:“ http:// localhost:8000 / api / v1 / systems ”),而且我也不确定运行应用程序的端口是否可以发生冲突或其他事情。
You will have to decide on a library to do API calls. 您将必须决定要执行API调用的库。 A simple way is to use
fetch
, which is built-in in modern browsers. 一种简单的方法是使用
fetch
,它是现代浏览器中内置的。 There's a polyfill to cover older ones. 有一个polyfill可以覆盖较旧的。 jQuery's AJAX or SuperAgent are two alternatives.
jQuery的AJAX或SuperAgent是两种选择。 Here's a simple example using
fetch
. 这是一个使用
fetch
的简单示例。 You'll only have to change the URL of the request. 您只需要更改请求的URL。
class Example extends React.Component { constructor() { super(); this.state = { data: {} }; } componentDidMount() { var self = this; fetch('http://reqres.in/api/users') .then(function(response) { return response.json() }).then(function(data) { self.setState({ data }, () => console.log(self.state)); }); } render() { return ( <div/> ); } } ReactDOM.render(<Example/>, document.getElementById('View'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="View"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.