在我的代码中,我有5个复选框,如果要选中它们,则要推送其值,但是当我勾选一个复选框,然后勾选另一个复选框时,将显示错误,提示 TypeError:无法读取属性的“值”空
这是我的代码:
| Date | Ticker | Open |
| ---------- | ------ | ------ |
| 2020-09-21 | MSFT | 197.19 |
| 2020-09-21 | ABF.L | 1903.5 |
| 2020-09-21 | ULVR.L | 4706 |
| 2020-09-22 | MSFT | 205.06 |
| 2020-09-22 | ABF.L | 1855 |
| 2020-09-22 | ULVR.L | 4671 |
| 2020-09-23 | MSFT | 207.9 |
| 2020-09-23 | ABF.L | 1870.5 |
| 2020-09-23 | ULVR.L | 4766 |
| 2020-09-24 | MSFT | 199.85 |
| 2020-09-24 | ABF.L | 1847 |
| 2020-09-24 | ULVR.L | 4743 |
答案 0 :(得分:1)
React使用合成事件,当您在if / else语句中尝试使用它时,它就消失了。
有三种解决方案:
e.persist()
开始changeHandler(只需在const changeHandler = (e) => {
之后添加它)。const target = e.target
开始changeHandler,然后使用target
代替e.target
。const value = e.target.value
开始changeHandler,然后使用value
代替e.target.value
working code (sandbox)。 react docs中的更多信息。
请注意,在React 17中,您将按预期使用e.target.value
(release notes)。