首页 > 解决方案 > React input when focus

问题描述

Just starting react and trying to follow Kirupa

When i click on submit button after entering some values to the input, all working good, but when i enter some value and hit keyboard enter i have some errors like : f

onloadwff.js:71 Uncaught TypeError: Cannot read property 'type' of undefined
at setFieldValue (onloadwff.js:71)
at HTMLFormElement.formKeydownListener (onloadwff.js:71)

From console, all still working but i don't know why the error.

Thanks for your helps guys

Thank you

标签: reactjs

解决方案


尝试将此添加到您的输入标签:

onKeyDown={() => event.keyCode === 13 ? this.addItem : ''}

(13 是输入的键码)

这会将 enter 事件绑定到您的 addItem 函数。因此,我不喜欢在表单中使用提交按钮。将您的输入信息存储在某种状态下,然后发送状态值更容易(对我来说,它的错误更少,更直接)。


推荐阅读