简体   繁体   English

React.js 中的每个映射项为 Function

[英]Function for each mapped item in React.js

I have a function, which makes axios request to backend and returns me data about the category by category_id.我有一个 function,它向后端发出 axios 请求,并按 category_id 返回有关类别的数据。 In two words, it returns me a length of elements in table with this parameters.简而言之,它使用此参数返回表中元素的长度。

async function getLength(category_id) {
    const res = await axios
      .post("/api/passwords/categories", {
        category_id,
        user_id,
      })
      .catch((err) => console.error(err));

    return res;
  }

  async function length(category_id) {
    const res = await getLength(category_id).then((data) => {
      return data.data.length;
    });

    console.log("WIP length: ", res);
    return res;
  }

I need for every mapped item make a request to function length() to take it's data:我需要为每个映射项目向 function length()发出请求以获取它的数据:

 {categories.map((x, index) => {
            return (
              <div key={index} className="col-xl-3 col-lg-3 col-md-3 col-6">
                <div className="dashboard__categories-item">
                  <span style={{ color: `${x.color}` }}>{x.icon}</span>
                  <h5>{x.title}</h5>
                  <h5>Length: {length(x.id)})</h5>
                </div>
              </div>
            );
          })}

But, I have an error: error message image但是,我有一个错误:错误信息图片

It makes my brain more than 3 hours.它使我的大脑超过3个小时。 Please, help me:D请帮帮我:D

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

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