[英]Dispaly an array of objects from an API in React
I'm trying to display an array of information on my page but I'm stuck on how I should do it.我试图在我的页面上显示一系列信息,但我不知道该怎么做。
I believe that the problem is in how I'm trying to render it but I have already done so many tries that I run out of ideas.我相信问题在于我如何尝试渲染它,但我已经做了很多尝试,以至于我没有想法。
In the console, I´m having success on the request and I receive the array, but I can't figure out how to display it.在控制台中,我的请求成功了,我收到了数组,但我不知道如何显示它。
import React, { useState } from 'react';
import { useNavigate } from "react-router-dom";
import axios from "axios";
const MyLocations = () => {
const [startDate, setStartDate] = useState("");
const [endDate, setEndDate] = useState("");
console.log(endDate);
console.log(startDate);
const tokenAtual = sessionStorage.getItem("token");
const [getLocations, setLocations] = useState([]);
const handleList = () => {
var axios = require("axios");
var data = '{\r\n "end": "2022-07-01",\r\n "start": "2022-01-01"\r\n}';
var config = {
method: "post",
url: "https://api.secureme.pt/api/v1/position/history",
headers: {
Authorization:
tokenAtual,
"Content-Type": "text/plain",
},
data: data,
};
axios(config)
.then(function (response) {
console.log(response.data);
setLocations(response.data);
return
<div>
<h1>getLocations.map(response.data.location.latitide)</h1>
<h1>getLocations.map(response.data.location.longitude)</h1>
</div>
})
.catch(function (error) {
console.log(error);
});
}
return (
<div>
<title>My Locations</title>
<input
type="button"
className="btn-pastLocals"
onClick={handleList}
value={"See Past Locations"}
/>
<input
type="date"
className="start-date"
placeholder="Start Date"
value={startDate}
onChange={(e) => setStartDate(e.target.value)}/>
<input
type="date"
className="end-date"
placeholder="End Date"
value={endDate}
onChange={(e) => setEndDate(e.target.value)}/>
</div>
);
}
export default MyLocations;
Can anyone please help me?谁能帮帮我吗? Thank you..!!
谢谢..!!
This might work if the the locations array has this structure如果位置数组具有这种结构,这可能会起作用
import React, { useState } from 'react';
import { useNavigate } from "react-router-dom";
import axios from "axios";
const MyLocations = () => {
const [startDate, setStartDate] = useState("");
const [endDate, setEndDate] = useState("");
console.log(endDate);
console.log(startDate);
const tokenAtual = sessionStorage.getItem("token");
const [getLocations, setLocations] = useState([]);
const handleList = () => {
var axios = require("axios");
var data = '{\r\n "end": "2022-07-01",\r\n "start": "2022-01-01"\r\n}';
var config = {
method: "post",
url: "https://api.secureme.pt/api/v1/position/history",
headers: {
Authorization:
tokenAtual,
"Content-Type": "text/plain",
},
data: data,
};
axios(config)
.then(function (response) {
console.log(response.data);
setLocations(response.data);
})
.catch(function (error) {
console.log(error);
});
}
return (
<div>
<title>My Locations</title>
{locations.map((location) => {
return (
<>
<h1>
{location.data.location.latitude}
</h1>
<h1>
{location.data.location.latitude}
</h1>
</>
)
})}
<input
type="button"
className="btn-pastLocals"
onClick={handleList}
value={"See Past Locations"}
/>
<input
type="date"
className="start-date"
placeholder="Start Date"
value={startDate}
onChange={(e) => setStartDate(e.target.value)}/>
<input
type="date"
className="end-date"
placeholder="End Date"
value={endDate}
onChange={(e) => setEndDate(e.target.value)}/>
</div>
);
}
export default MyLocations;
First of all, you need to study the react documentation and learn how it works.首先,您需要研究 react 文档并了解它是如何工作的。
Secondly, here is a possible solution to your problem:其次,这是您的问题的可能解决方案:
return (
<div>
<title>My Locations</title>
{
getLocations.map(
location => (
<div>
<h1>
{location.data.location.latitude}
</h1>
<h1>
{location.data.location.latitude}
</h1>
</div>
)
)
}
etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.