Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React中从查询选择器工作到useRef?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在React中从查询选择器工作到useRef?

Javascript 如何在React中从查询选择器工作到useRef?,javascript,reactjs,Javascript,Reactjs,我有一个打字效果函数。现在我想向代码显示div typingRef中的效果,我制作了类似于typingRef.current=letter的东西,但这与document.queryselector'typing.textContent=letter相同吗?我想使用ref而不是queryselector 我能想出的最好办法就是试着理解你的问题。试试这个:typingRef.current.innerHTML export const Text = ({ children }) => {

我有一个打字效果函数。现在我想向代码显示div typingRef中的效果,我制作了类似于typingRef.current=letter的东西,但这与document.queryselector'typing.textContent=letter相同吗?我想使用ref而不是queryselector


我能想出的最好办法就是试着理解你的问题。试试这个:typingRef.current.innerHTML

export const Text = ({ children }) => {
  const headingRef = React.useRef(null)
  const typingRef = React.useRef(null)
  const texts = ['websites', 'applications', 'mobile' ]
  React.useEffect(() => {
    let count = 0
    let index = 0
    let currentText = ''
    let letter = ''

    function type() {
      if (count === texts.length) {
        count = 0
      }
      currentText = texts[count]
      letter = currentText.slice(0, ++index)

      typingRef.current.innerHTML = letter

      if (letter.length === currentText.length) {
        count++
        index = 0
      }
      setTimeout(type, 400)
    }
    type()
  })
  return (
    <div>
      <div>
        <h1 ref={headingRef}>
          { children }
        </h1>
        <div ref={typingRef}/>
      </div>
    </div>
  )
}
export const Text = ({ children }) => {
  const headingRef = React.useRef(null)
  const typingRef = React.useRef(null)
  const texts = ['websites', 'applications', 'mobile' ]
  React.useEffect(() => {
    let count = 0
    let index = 0
    let currentText = ''
    let letter = ''

    function type() {
      if (count === texts.length) {
        count = 0
      }
      currentText = texts[count]
      letter = currentText.slice(0, ++index)

      typingRef.current.innerHTML = letter

      if (letter.length === currentText.length) {
        count++
        index = 0
      }
      setTimeout(type, 400)
    }
    type()
  })
  return (
    <div>
      <div>
        <h1 ref={headingRef}>
          { children }
        </h1>
        <div ref={typingRef}/>
      </div>
    </div>
  )
}