首页 > 解决方案 > 反应处理 api 参数未显示

问题描述

这是我的专栏。
我需要来自 api 的包含客户 ID 的 href。我目前正在使用 reactTable,一切正常。但是当我查看控制台内部时,只有参数没有被处理。谁能告诉我缺少什么?

const columns = [
  {
    Header: '',
    accessor: 'links[0].href',
    filterable: false,
    sortable: false,
    Cell: ({params}) => <Button color="secondary" onClick={()=>deleteCustomer(params)}>Delete</Button>
  }
];

const deleteCustomer = (params) => {
  console.log(params)
};

const getCustomer = () => {
  fetch('https://customerrest.herokuapp.com/api/customers')
    .then(response => response.json())
    .then(data => setCustomer(data.content))
    .catch(err => console.error(err))
};

这是一个示例 API 调用:

    {
      "links" : [ {
        "rel" : "self",
        "href" : "https://customerrest.herokuapp.com/api/customers"
      }, {
        "rel" : "profile",
        "href" : "https://customerrest.herokuapp.com/api/profile/customers"
      } ],
      "content" : [ {
        "firstname" : "Mark",
        "lastname" : "Johnson",
        "streetaddress" : "5th Street",
        "postcode" : "23110",
        "city" : "Flintsone",
        "email" : "john@mail.com",
        "phone" : "232-2345540",
        "content" : [ ],
        "links" : [ {
          "rel" : "self",
          "href" : "https://customerrest.herokuapp.com/api/customers/620"
        }, {
          "rel" : "customer",
          "href" : "https://customerrest.herokuapp.com/api/customers/620"
        }, {
          "rel" : "trainings",
          "href" : "https://customerrest.herokuapp.com/api/customers/620/trainings"
        } ]
      } 

标签: reactjsapiparameters

解决方案


在您的 onClick 句柄函数中传递参数:

Cell: ({params}) => <Button color="secondary" onClick={(params)=>deleteCustomer(params)}>Delete</Button>

推荐阅读