React类型测试仪仅接收一个输入然后停止

发布于 2025-02-09 06:08:53 字数 954 浏览 1 评论 0原文

因此,我不确定为什么我的代码不会进入后续的键盘,它需要第一个“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-02-16 06:08:53
import { useState } from 'react'

const Input = ({ text }) => {
  const [lorem, changeString] = useState(text.split(''))
  const [position, changePosition] = useState(0)
  const [color, changeColor] = useState(false)

  const keyDown = (e) => {
    if (e.key !== lorem[position]) {
      changeColor(true)
      return
    }
    changeString(lorem.slice(1))
    changePosition(position + 1)
  }



  return (
    <input
      className={`text ${color ? 'new' : ''}`}
      type="text"
      size={10000}
      onKeyDown={keyDown}
    ></input>
  )
}

export default Input
import { useState } from 'react'

const Input = ({ text }) => {
  const [lorem, changeString] = useState(text.split(''))
  const [position, changePosition] = useState(0)
  const [color, changeColor] = useState(false)

  const keyDown = (e) => {
    if (e.key !== lorem[position]) {
      changeColor(true)
      return
    }
    changeString(lorem.slice(1))
    changePosition(position + 1)
  }



  return (
    <input
      className={`text ${color ? 'new' : ''}`}
      type="text"
      size={10000}
      onKeyDown={keyDown}
    ></input>
  )
}

export default Input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文