【问题标题】:React ternary operator how to call 2 functions?React三元运算符如何调用2个函数?
【发布时间】:2022-11-03 06:08:54
【问题描述】:

如何在三元运算符中调用两个函数? 我通过添加一个 && 来尝试它,但它只调用第一个函数并忽略第二个函数。

e.g.
setState(date) && dispatch(paypal_error({ time: date }))

这是完整的代码

  const handleDateChange = (date) => {
    parseInt(dayjs(date).format('HH') * 60) +
      parseInt(dayjs(date).format('m')) >
      pauseStartMin &&
    parseInt(dayjs(date).format('HH') * 60) +
      parseInt(dayjs(date).format('m')) <
      pauseEndMin ? (
      <>
        <>
          {setState(
            new Date(0, 0, 0, pauseEndHour, pauseEndMinute + deliveryTime)
          ) &&
            dispatch(
              paypal_error({
                time: new Date(
                  0,
                  0,
                  0,
                  pauseEndHour,
                  pauseEndMinute + deliveryTime
                ),
              })
            )}
        </>
      </>
    ) : (
      setState(date) && dispatch(paypal_error({ time: date }))
    );
  };

【问题讨论】:

    标签: javascript reactjs conditional-operator


    【解决方案1】:

    使用comma operator

    (setState(date), dispatch(paypal_error({ time: date })))
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2018-12-04
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2014-01-19
      • 2020-04-30
      • 2019-07-30
      相关资源
      最近更新 更多