简体   繁体   English

在 React 中显示来自 API 的对象数组

[英]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.

相关问题 在 React 中只显示来自 Rest api 的单个对象数组 - Only show single array of objects from Rest api in React 在 React 中使用 axios 在浏览器数组中显示来自 api 的对象 - Display on browser array of objects from api using axios in React 如何在 React js 中访问 api 中的对象数组? - How to access array of objects from the api in react js.? 如何在从 Api react 获得响应后将多个对象添加到空数组中 react - How to add multiple Objects to an empty array react after getting response from Api react 仅对来自 React 中 api 响应的对象数组中的几个对象进行排序 - Sort only few objects from an array of object coming from api response in React 问题映射来自 API 的数据,因为它只是一个对象而不是 React 中的对象数组 - Problem mapping over data from API as its just an object not an array of objects in React React JS / Node JS:从获取 API 响应中映射数组与内部对象的问题 - React JS / Node JS : Problem mapping array with objects inside from fetch API response 在将 API 中的对象数组传递给 React 组件之前,如何对它们进行排序? - How can I sort an array of objects from an API before passing them into a react component? React - 从 API 映射对象数组时处理未定义状态 - React - Dealing with undefined state when mapping over an array of objects from API 反应如何调用 API 并在对象数组中显示数组 PokeAPI - React how to make a call to an API and display an array in an array of objects PokeAPI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM