React类型测试仪仅接收一个输入然后停止
因此,我不确定为什么我的代码不会进入后续的键盘,它需要第一个“ L”,然后停止:
老实说,我不知道问题是什么。 Lorem是我变成数组的长字符串,如果“ E.Key” === Array在0处,请删除该项目并更新位置。
我不知道为什么每个键盘都没有完成。
import { useState } from "react"
const Input = ({text}) => {
const [lorem,changeString] = useState(text.split(''))
const [input,changeInput] = useState('')
const [position,changePosition] = useState(0)
const [color,changeColor] = useState(false)
const keyDown = (e) => {
changeInput(e.key)
compareCharacters(input)
}
const compareCharacters = (input) => {
if (input === lorem[position]) {
changeString(lorem.splice(0,1))
changePosition(position+1)
} else {
changeColor(true)
}
}
return (
<input className={`text ${color ? 'new' : ''}`}
type="text"
size="10000"
onKeyDown={keyDown}
>
</input>
)
}
export default Input
So I'm not sure why my code isn't taking into subsequent keydowns, it takes the first 'l' then just stops:
I honestly don't know what the issue is. The lorem is a long string that I turn into an array, then if "e.key" === array item at 0, remove that item and update position.
I don't know why this isn't being done for every keydown.
import { useState } from "react"
const Input = ({text}) => {
const [lorem,changeString] = useState(text.split(''))
const [input,changeInput] = useState('')
const [position,changePosition] = useState(0)
const [color,changeColor] = useState(false)
const keyDown = (e) => {
changeInput(e.key)
compareCharacters(input)
}
const compareCharacters = (input) => {
if (input === lorem[position]) {
changeString(lorem.splice(0,1))
changePosition(position+1)
} else {
changeColor(true)
}
}
return (
<input className={`text ${color ? 'new' : ''}`}
type="text"
size="10000"
onKeyDown={keyDown}
>
</input>
)
}
export default Input
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)