首页 > 解决方案 > 在 JavaScript 中将 const 转换为函数 - React

问题描述

如何将他的常数转换为函数?我认为最好有一个常数这么长的函数。它先做一个过滤器,然后再做一个地图。

const cells = upcoming
  .filter(clinic => {
    if (showAll) return true;
    if (
      clinic.staff.teamLeadStaffed !== null &&
      clinic.staff.techsStaffed.length === clinic.staff.techsNeeded
    )
      return false;
    return true;
  })
  .map(clinic => {
    const [color, reason] = this.getColorAndReason(clinic);

我尝试过这样的事情,但没有奏效

  renderCell(upcoming) {
    return (
      upcoming
        .filter(clinic => {
          if (showAll) return true;
          if (
            clinic.staff.teamLeadStaffed !== null &&
            clinic.staff.techsStaffed.length === clinic.staff.techsNeeded
          )
            return false;
          return true;
        })
        .map(clinic => {
          const [color, reason] = this.getColorAndReason(clinic);
    )
  }

标签: javascriptreactjsfunction

解决方案


function cells() {
  return upcoming
    .filter(clinic => {
      if (showAll) return true;
      if (
        clinic.staff.teamLeadStaffed !== null &&
        clinic.staff.techsStaffed.length === clinic.staff.techsNeeded
      )
        return false;
      return true;
    })
    .map(clinic => {
      const [color, reason] = this.getColorAndReason(clinic);
    });
}

推荐阅读