reactjs - 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
解决方案
尝试将此添加到您的输入标签:
onKeyDown={() => event.keyCode === 13 ? this.addItem : ''}
(13 是输入的键码)
这会将 enter 事件绑定到您的 addItem 函数。因此,我不喜欢在表单中使用提交按钮。将您的输入信息存储在某种状态下,然后发送状态值更容易(对我来说,它的错误更少,更直接)。
推荐阅读
- macos - 如何在mac os中构建一个可以将文件从主机复制到容器的docker镜像?
- sql - 如何从具有多个表的查询中创建不同的计数
- javascript - 我如何使用 Promise.all 使用此参数执行多个请求?
- java - 在anyRequest之后无法配置antMatchers?如何解决
- clojure - 使用“Clojure”的基本计算器
- python - ImportError 的原因:No module named requests
- python - 更新了 Anaconda 但它坏了,所以卸载并再次安装但它不起作用?
- javascript - 如何在不使用 cookie 或 service worker 的情况下在每个请求中发送令牌?
- python - 从句子中删除第一个单词并返回剩余的字符串
- javascript - Promise 安排和语法