在地图功能中调用API

发布于 2025-01-29 10:25:51 字数 1773 浏览 5 评论 0 原文

我是我想在地图功能中调用API的应用程序的新手,我认为我必须在该应用程序中调用API。

首先,我调用一个API,该API返回以这样的kendo表单渲染的元素列表:

import { Form, Field, FormElement } from "@progress/kendo-react-form";
...
const [tempAtr, setTempAtr] = useState([]);
...
const resAtr = await services.getTemplateSessionAtributi(sesijaV);
setTempAtr(resAtr.data.rlista); //tempAtr is filled with a list of elements (attributes)
...
{tempAtr.map((data) =>
<Field
                required={data.required=== 1 ? true : false}
                disabled={data.read_only === 1 ? true : false}
                name={data.name}
                component={ 
                      data.tpodatka_id === 1 ?Input: 
                      data.tpodatka_id === 2 ?Input: 
                      data.tpodatka_id === 3 ?DatePicker: 
                      data.tpodatka_id === 4 ?DropDownList:
                      data.tpodatka_id === 5 ?TextArea :
                      data.tpodatka_id === 6 ?DropDownList:
                      data.tpodatka_id === 7 ?DropDownList:
                      data.tpodatka_id === 8 ?MultiSelect:
                      data.tpodatka_id === 9 ?MultiSelect:
                      Input}
                label={data.name}
                data={ 
                      
                      data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? 

**call an API and fetch data to fill the dropdown for that specific attribute id ** : 
                      null
                }
                textField={data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? "name" : null}
                dataItemKey={data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? "id" : null}
              />)}

因此,在数据部分中,我必须将attribut_id的API称为一个参数知道该怎么做?可以有一个下拉属性的1或5或15,因此,我唯一能想到的就是调用映射功能中的API并在返回时分配值。

帮助任何人?

I am fairly new to React and am making an app in which I have to call an API inside the map function or so I think.

First, I call an API which returns a list of elements that are rendered in a kendo form like this:

import { Form, Field, FormElement } from "@progress/kendo-react-form";
...
const [tempAtr, setTempAtr] = useState([]);
...
const resAtr = await services.getTemplateSessionAtributi(sesijaV);
setTempAtr(resAtr.data.rlista); //tempAtr is filled with a list of elements (attributes)
...
{tempAtr.map((data) =>
<Field
                required={data.required=== 1 ? true : false}
                disabled={data.read_only === 1 ? true : false}
                name={data.name}
                component={ 
                      data.tpodatka_id === 1 ?Input: 
                      data.tpodatka_id === 2 ?Input: 
                      data.tpodatka_id === 3 ?DatePicker: 
                      data.tpodatka_id === 4 ?DropDownList:
                      data.tpodatka_id === 5 ?TextArea :
                      data.tpodatka_id === 6 ?DropDownList:
                      data.tpodatka_id === 7 ?DropDownList:
                      data.tpodatka_id === 8 ?MultiSelect:
                      data.tpodatka_id === 9 ?MultiSelect:
                      Input}
                label={data.name}
                data={ 
                      
                      data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? 

**call an API and fetch data to fill the dropdown for that specific attribute id ** : 
                      null
                }
                textField={data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? "name" : null}
                dataItemKey={data.tpodatka_id === (4 || 6 || 7 || 8 || 9) ? "id" : null}
              />)}

so in the data section I must call an API with a attribut_id as a parameter so I can fetch dropdown values for that attribute and I don't know how to do it? There can be 1 or 5 or 15 of dropdown attributes, so the only thing I can think of is calling the API inside the map function and assigning values upon it's return.

Help anybody?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2025-02-05 10:25:51

最佳选择是提取下拉单独的React组件&amp;在其使用中进行API调用。您可以将 id 作为道具传递。示例(

import React from "react";
import "./styles.css";

function YearDropDown(props) {
  const [loading, setLoading] = React.useState(true);
  const [items, setItems] = React.useState([]);
  const [value, setValue] = React.useState("2013");
  const { drilldowns } = props;
  React.useEffect(() => {
    let unmounted = false;
    async function getCharacters() {
      const response = await fetch(
        `https://datausa.io/api/data?drilldowns=${drilldowns}&measures=Population`
      );
      const body = await response.json();
      if (!unmounted) {
        setItems(body.data.map(({ Year }) => ({ label: Year, value: Year })));
        setLoading(false);
      }
    }
    getCharacters();
    return () => {
      unmounted = true;
    };
  }, [drilldowns]);

  return (
    <select
      disabled={loading}
      value={value}
      onChange={(e) => setValue(e.currentTarget.value)}
    >
      {items.map(({ label, value }) => (
        <option key={value} value={value}>
          {label}
        </option>
      ))}
    </select>
  );
}

export default function App() {
  return (
    <div className="App">
      <YearDropDown drilldowns="Nation" />
    </div>
  );
}

Best option is to extract the dropdown a separate react component & make the API call in it's useEffect. You can pass the id as a prop. Example (Codesandbox)

import React from "react";
import "./styles.css";

function YearDropDown(props) {
  const [loading, setLoading] = React.useState(true);
  const [items, setItems] = React.useState([]);
  const [value, setValue] = React.useState("2013");
  const { drilldowns } = props;
  React.useEffect(() => {
    let unmounted = false;
    async function getCharacters() {
      const response = await fetch(
        `https://datausa.io/api/data?drilldowns=${drilldowns}&measures=Population`
      );
      const body = await response.json();
      if (!unmounted) {
        setItems(body.data.map(({ Year }) => ({ label: Year, value: Year })));
        setLoading(false);
      }
    }
    getCharacters();
    return () => {
      unmounted = true;
    };
  }, [drilldowns]);

  return (
    <select
      disabled={loading}
      value={value}
      onChange={(e) => setValue(e.currentTarget.value)}
    >
      {items.map(({ label, value }) => (
        <option key={value} value={value}>
          {label}
        </option>
      ))}
    </select>
  );
}

export default function App() {
  return (
    <div className="App">
      <YearDropDown drilldowns="Nation" />
    </div>
  );
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文