简体   繁体   English

onChange 没有被调用 React

[英]onChange not getting called React

I'm trying to debug a react checkbox and the onChange event doesn't ever get called.我正在尝试调试一个反应复选框,并且永远不会调用 onChange 事件。 I have added a console.log to test and this doesn't ever run.我添加了一个 console.log 来测试,但它永远不会运行。 Here is the code for the checkbox.这是复选框的代码。 What is the issue?问题是什么?

return (
    <div className="RampPane">
      <div className="RampPane--content">
        <p className="RampText">{transaction.merchant} </p>
        <b>{moneyFormatter.format(transaction.amount)}</b>
        <p className="RampText--hushed RampText--s">
          {transaction.employee.firstName} {transaction.employee.lastName} - {transaction.date}
        </p>
      </div>
      <InputCheckbox
        id={transaction.id}
        checked={approved}
        disabled={loading}
        onChange={async (newValue) => {
          console.log("click")
          await consumerSetTransactionApproval({
            transactionId: transaction.id,
            newValue,
          })

          setApproved(newValue)
        }}
      />
    </div>
  )

Here is the InputCheckBox Component这是 InputCheckBox 组件

return (
    <div className="RampInputCheckbox--container" data-testid={inputId}>
      <label
        className={classNames("RampInputCheckbox--label", {
          "RampInputCheckbox--label-checked": checked,
          "RampInputCheckbox--label-disabled": disabled,
        })}
      />
      <input
        id={inputId}
        type="checkbox"
        className="RampInputCheckbox--input"
        checked={checked}
        disabled={disabled}
        onChange={() => onChange(!checked)}
      />
    </div>
  )

Use this for InputCheckBox Component将其用于 InputCheckBox 组件

return (
    <div className="RampInputCheckbox--container" data-testid={inputId}>
      <label
        className={classNames("RampInputCheckbox--label", {
          "RampInputCheckbox--label-checked": checked,
          "RampInputCheckbox--label-disabled": disabled,
        })}
        htmlFor={inputId}
      />
      <input
        id={inputId}
        type="checkbox"
        className="RampInputCheckbox--input"
        checked={checked}
        disabled={disabled}
        onChange={() => onChange(!checked)}
      />
    </div>
  )

This works!这行得通!

If the InputCheckBox is a custom built react component that renders an html input element try checking that the onChange handler is passed to the root component correctly IF Not try including the component here to get a better insight如果 InputCheckBox 是一个自定义构建的反应组件,它呈现一个 html 输入元素,请尝试检查 onChange 处理程序是否正确传递给根组件如果不尝试在此处包含该组件以获得更好的洞察力

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

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