【问题标题】:Expected an assignment or function call and instead saw an expression no-unused-expressions- ReactJs期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions-ReactJs
【发布时间】:2020-04-21 02:41:46
【问题描述】:

我正在尝试创建一个预订应用程序。我对 React 很陌生,我尝试搜索,但似乎找不到问题所在。 由于我的代码有 400 多行,我将只发布我遇到错误的那些。 我在关注这篇文章https://medium.com/@kris101/building-appointment-scheduler-app-in-react-and-nodejs-d01f7294a9fd

Error screenshot

具体错误有:

第 136:11 行:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions 第 139:11 行:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions 第 147:7 行:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions

我尝试禁用 eslint,但是当我这样做时,我遇到了更多错误。

handleDBReponse(response) {
    const reservations = response;
    const today = moment().startOf("day"); //start of today 12 am
    const initialSchedule = {};
    initialSchedule[today.format("YYYY-DD-MM")] = true;
    const schedule = !reservations.length
      ? initialSchedule
      : reservations.reduce((currentSchedule, reservation) => {
          const { slot_date, slot_time } = reservation;
          const dateString = moment(slot_date, "YYYY-DD-MM").format(
            "YYYY-DD-MM"
          );
          !currentSchedule[slot_date]
            ? (currentSchedule[dateString] = Array(8).fill(false))
            : null;
          Array.isArray(currentSchedule[dateString])
            ? (currentSchedule[dateString][slot_time] = true)
            : null;
          return currentSchedule;
        }, initialSchedule);

    for (let day in schedule) {
      let slots = schedule[day];
      slots.length
        ? slots.every(slot => slot === true) ? (schedule[day] = true) : null
        : null;
    }

    this.setState({
      schedule: schedule
    });
  }

【问题讨论】:

  • 在哪一行导致错误?
  • 136 !currentSchedule[slot_date] 139 Array.isArray(currentSchedule[dateString]) 147 slot.length
  • 你能把错误截图加到帖子里吗?
  • 我更新了帖子:)

标签: reactjs


【解决方案1】:

我可以在上面的代码中看到很多 linting 问题。到目前为止,我已经修复了预期的赋值或函数调用问题。

请检查下面的代码,如果它有效,请告诉我

handleDBReponse(response) {
    const reservations = response;
    const today = moment().startOf('day'); // start of today 12 am
    const initialSchedule: any = {};
    initialSchedule[today.format('YYYY-DD-MM')] = true;
    const schedule = !reservations.length
        ? initialSchedule
        : reservations.reduce((currentSchedule: any, reservation: any) => {
            const tempSchedule = currentSchedule;
            const { slot_date, slot_time } = reservation;
            const dateString = moment(slot_date, 'YYYY-DD-MM').format('YYYY-DD-MM');
            tempSchedule[dateString] = !tempSchedule[slot_date] ? (tempSchedule[dateString] = Array(8).fill(false)) : null;
            if (Array.isArray(tempSchedule[dateString])) {
                tempSchedule[dateString][slot_time] = true;
            }
            return test;
        }, initialSchedule);

    for (const day in schedule) {
        const tempSchedule = schedule;
        const slots = tempSchedule[day];
        const checkEverySlot = slots.every((slot: boolean) => slot === true);
        if (slots.length && checkEverySlot) {
            tempSchedule[day] = true;
        }
    }

    this.setState({
        schedule,
    });
}

注意:最好将标识符写成驼峰式。

【讨论】:

  • 它仍然在这一行 slot.length 生成它? (slots.every((slot) => slot === true) ? (schedule[day] = true) : null) : null;
  • @Victoria 我已经更新了代码,请立即查看。
  • Dude It seams 整个应用程序有 eslint 问题。你需要一一修复。如果您的应用需要及时交付,那么我建议您暂时禁用 eslint 规则。
  • 您忘记在 App.jsx 中导入它。从'react'导入反应,{组件};
  • 如果我这样做了,它说找不到模块:无法解析 'D:\Faculta\WT\proj\api\reservation-scheduler\src\components' 中的 'moment' 但没有文件名为 moment
【解决方案2】:

这个问题是关于你使用三元语句的;

!currentSchedule[slot_date] ? (currentSchedule[dateString] = Array(8).fill(false)) : null;
Array.isArray(currentSchedule[dateString]) ? (currentSchedule[dateString[slot_time] = true) : null
slots.length ? slots.every(slot => slot === true) ? (schedule[day] = true) : null : null;

你不能以这种方式使用三元语句,你应该用单独的 if 语句替换它们

例如

if(currentSchedule[slot_date]) currentSchedule[dateString] = Array(8).fill(false)

正确使用三元组的一个例子是在分配变量时:

let myVar = x === 10 ? 'x is 10' : 'x is not 10'

【讨论】:

【解决方案3】:

函数handleDBReponse 似乎不是一个定义明确的函数。尝试在其名称前添加 public。喜欢public handleDBReponse(response) {...}

【讨论】:

  • 我试过了,它说 Line 124:10: Parsing error: Unexpected token
  • public 在 vanilla javascript 中是无效语法。
猜你喜欢
  • 2020-11-20
  • 2021-11-17
  • 2021-08-23
  • 2019-08-15
  • 2020-12-10
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多